将“<”和“>”写入xml文件而不是< 和 > 在 Java 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24908251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
writing "<" and ">" to a xml file instead of < and > in java
提问by TheUser
i have to write a few lines to a xml file which should contain < and > symbols as part of value of a tag. i am setting them in a string that has some text along with < and > symbols , and after marshalling through jaxb the xml which gets created has ;lt; and ;gt; instead of the < and > symbols. i tried using escape characters and ascii 60 and 62 values to get them as < and > but didn't work. Can anyone suggest some solution for this.
我必须向一个 xml 文件写几行,该文件应该包含 < 和 > 符号作为标签值的一部分。我将它们设置在一个字符串中,该字符串包含一些文本以及 < 和 > 符号,在通过 jaxb 编组后,创建的 xml 具有 ;lt; 和;gt; 而不是 < 和 > 符号。我尝试使用转义字符和 ascii 60 和 62 值将它们作为 < 和 > 但没有用。任何人都可以为此提出一些解决方案。
回答by T.J. Crowder
...the xml which gets created has ;lt; and ;gt; instead of the
<
and>
symbols...
...创建的 xml 具有 ;lt; 和;gt; 而不是
<
和>
符号...
I assume you mean <
and >
. That's correct. That's how you write <
and >
in XML text (although you're allowed to use >
literally as well, see below). They're called named character entities.
我假设你的意思是<
和>
。没错。这就是您在 XML 文本中编写<
和编写的方式>
(尽管您也可以按>
字面意思使用,请参见下文)。它们被称为命名字符实体。
This:
这个:
<foo>6 < 7</foo>
...defines an XML element with the content "6 < 7" in it.
...定义一个内容为“6 < 7”的 XML 元素。
This:
这个:
<foo>6 < 7</foo>
is invalid XML that will not parse.
是无法解析的无效 XML。
An alternative to character entities is to use a CDATA section:
字符实体的替代方法是使用 CDATA 部分:
<foo><![CDATA[6 < 7]]></foo>
Everything between the <![CDATA[
and ]]>
is treated as raw text, not XML. But note that the resulting XML DOM is slightly different. The foo
element's value is a CDATA section, which in turn has the value of "6 < 7"
. So it's less direct. When you use the character entity, foo
's value is "6 < 7"
(without any intervening bit).
<![CDATA[
和之间的所有内容都]]>
被视为原始文本,而不是 XML。但请注意,生成的 XML DOM 略有不同。的foo
元素的值是一个CDATA段,这反过来又具有的值"6 < 7"
。所以比较不直接。当您使用字符实体时,foo
的值为"6 < 7"
(没有任何中间位)。
Technically, you can write >
in the XML literally if you like. These two lines define exactly the same thing:
从技术上讲,>
如果您愿意,可以按字面意思用 XML编写。这两行定义了完全相同的东西:
<foo>7 > 6</foo>
<foo>7 > 6</foo>
Both are valid, and the resulting data when you parse it is exactly the same. But you normally see the first rather than the second.
两者都是有效的,解析时得到的数据完全相同。但是您通常会看到第一个而不是第二个。
Side note: &
must also be written using a character entity, since &
is what character entities start with. So to have an &
in text, you use &
:
旁注:&
还必须使用字符实体编写,因为&
字符实体以字符实体开头。所以要有一个&
文本,你使用&
:
<foo>Jones & Cooper Co.</foo>
回答by maress
You can also do this:
你也可以这样做:
<xml>
<![CDATA[
<data>sdsjddjdsdjs</data>
]]>
</xml>
回答by CatSpy
if we suppose that el is your element then: from lxml import etree
如果我们假设 el 是你的元素,那么: from lxml import etree
el.text = etree.CDATA('a string')