将 < 转换为 < xml 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2399451/
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
convert < to < xml document
提问by boom
I have read an XML file and converted into NSXMLDocument object. But, due to the presence of "<" in the string content of a node, it has been converted into "<". So, when i write it as xml document to a file, it has the character "<" in it.
我已经阅读了一个 XML 文件并转换为 NSXMLDocument 对象。但是,由于节点的字符串内容中存在“<”,它已被转换为“<”。因此,当我将其作为 xml 文档写入文件时,其中包含字符“<”。
How can i write to the file as ordinary XML file in which "<" will be replaced by "<".
如何将“<”替换为“<”的普通 XML 文件写入文件。
Thanks and Regards, Lenin
感谢和问候,列宁
采纳答案by Lachlan Roche
When the <character appears in a text node, it will be serialized as <when you write your xml to a file.
当<字符出现在文本节点中时,它将被序列化,就像<您将 xml 写入文件时一样。
When you later read that file with an xml parser, the text node will contain the original <character.
当您稍后使用 xml 解析器读取该文件时,文本节点将包含原始<字符。
You also have the option of CDATA encoding the text node. In this case the serialized character is <inside a CDATA block. Many XML APIs will not retain any difference between CDATA and text nodes. CDATA should be thought of as an escaping mechanism to make editing easy for human authors.
您还可以选择对文本节点进行 CDATA 编码。在这种情况下,序列化字符<位于 CDATA 块内。许多 XML API 不会保留 CDATA 和文本节点之间的任何区别。CDATA 应该被认为是一种转义机制,使人类作者可以轻松进行编辑。
回答by Amarghosh
The <in text nodes of an xml should be represented as <.
将<在XML文本节点应该被表示为<。
If you replace it using s/</</gbefore writing it to the xml file, it will lead to parsing error when you read from that file.
如果在将其s/</</g写入 xml 文件之前使用替换它,则在读取该文件时会导致解析错误。
回答by Pavunkumar
You can use following function for doing the vice versa operation
您可以使用以下功能进行反之亦然的操作
htmlspecialchars and
htmlentities()
htmlspecialchars 和
htmlentities()
回答by Mike Cialowicz
The <character must be escaped when it's not part of an XML tag (see here). You really shouldn't break the XML spec...
如果该<字符不是 XML 标记的一部分,则必须对其进行转义(请参阅此处)。你真的不应该破坏 XML 规范......
However, if you must, you can perform a trick before writing your XML document:
但是,如果必须,您可以在编写 XML 文档之前执行一个技巧:
- Replace your instances of
<with a unique string that won't be escaped by your XML parser (something that you know occurs nowhere else in the file). Something like*-lt-*will probably do. - Have the parser produce the file & save it.
- Read in the file as
plaintext, and replace your instances
of
*-lt-*with the regular<character. - Re-write the file, clobbering the version that was written by the XML parser.
<用一个不会被你的 XML 解析器转义的唯一字符串替换你的实例(你知道的东西在文件的其他地方不会出现)。类似的事情*-lt-*可能会做。- 让解析器生成文件并保存它。
- 以纯文本形式读入文件,并用
*-lt-*常规<字符替换您的实例。 - 重新编写文件,破坏由 XML 解析器编写的版本。
Again, it's a terrible idea to break the XML spec... by doing this, no parser will be able to read your new XML document.
同样,打破 XML 规范是一个糟糕的主意……通过这样做,解析器将无法读取您的新 XML 文档。
回答by Laurynas
If you want to replace < to < in XML document via php:
如果您想通过 php 将 XML 文档中的 < 替换为 <:
$xmlFile = file_get_contents("example.xml");
$xml= str_replace(['<','>'],['<','>'], $xmlFile);
echo $xml;
I am getting news feed from a server via XML and then store it to another xml file, I'm geting the result with <and >so I used this method. It's not a smartest way to store XML but I am not a pro mega senior back ender either.
我从通过XML服务器获取新闻提要,然后将其保存到另一个XML文件,我在歌厅的结果与<和>,所以我用这个方法。这不是存储 XML 的最聪明的方法,但我也不是专业的大型高级后端。

