< & > are converted to > < etc

廉价感情. 提交于 2020-01-16 09:09:11

问题


I am using MSXMl library to parse xml

after I call put_text and then get_xml

the output will have < & > converted to &lt; & &gt;

How can i get rid of this?


回答1:


< and > are prohibited to use inside the text and need to be encoded as &gt and &lt. The only way to avoid this is creating a CDATA section for the text containing those. But you really don't need to if you intend to read the XMLT with MS XML - it will decode those symbols just fine and you will get your < and > perfectly fine in the extracted text.




回答2:


Well you are converting from plain text to XML encoded text.
This is the behavior I would expect.

If you want the original string you put in try converting back to text with get_text().

If you do not want the put_text() to encode the text without encoding the < and > then it must be inside a CData section.

<![CDATA[    Text that can include < and > without encoding  ]]>



回答3:


Are you getting &lt or &lt; ?

There should be a semi-colon on the end of them to be a valid entity.




回答4:


If if you want it to be included as text its fine that it is escaped. Each xml pareser will back escape the text.

If you want it as xml elements you can not create them using put_text but need to create the tree this way

dataNode=xmlDoc.createElement("data")

idNode=xmlDoc.createElement("id")
textNode=xmlDoc.createTextNode("17")
idNode.appendChild(textNode)
nameNode=xmlDoc.createElement("name")
textNode=xmlDoc.createTextNode("Uday")
nameNode.appendChild(textNode)
...

dataNode.appendChild(idNode)
dataNode.appendChild(nameNode)
...

parentNode.appendChild(dataNode)

what might looker better if you look with the eye and want the text be written on the file you may use cdata section.

newCDATA=xmlDoc.createCDATASection("<data><id>17</id>...</data>")
parentNode.appendChild(newCDATA)


来源:https://stackoverflow.com/questions/1011392/are-converted-to-gt-lt-etc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!