tags:

views:

674

answers:

4

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?

+3  A: 

< 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.

sharptooth
but when i use LoadXMl for the same text it returnn an error :(
Uday
Which error exactly?
sharptooth
Here is what exactly i am trying to do <Data> <Id>17</Id> <Name>Uday</Name> <address> <Data><pin>581</pin></Data> </address></Data> I create address node and add Data xml using put_text (which is xml not text) Then add address node to outer Data node That is why my text is getting converted i think.can you confirm this?
Uday
If you want it to be part of the xml document you can not create it by creating a string and then attach it to the xml tree but you have to create the related child elements.
Totonga
Yes, exactly. You need to assemble the tree structure in memory.
sharptooth
Ok thanks alot sharptooth
Uday
A: 

Are you getting &lt or &lt; ?

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

Peter Boughton
This should have been a comment, as it isn't an answer.
Joey
Peter Boughton
+2  A: 

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  ]]>
Martin York
A: 

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)
Totonga