BaH.libxml bug converting '&'

BlitzMax Forums/Brucey's Modules/BaH.libxml bug converting '&'

TomToad(Posted 2013) [#1]
If you run this code
Import BaH.libxml

Local XmlDoc:TxmlDoc = TxmlDoc.NewDoc("1.0")
Local Node:TxmlNode = TxmlNode.Newnode("story")
XmlDoc.SetRootElement(Node)
Node.AddChild("code1",Null,"<1>")
Node.AddChild("code2",Null,"<2>")
Node.AddChild("code3",Null,"<1&2>") 'This line produces an error
XmlDoc.SaveFormatFile("test.xml",True,"utf-8")

It will generate the error error : unterminated entity reference 2>


If you look at the resulting file, you will see that '<' and '>' are converted to '&lt;' and '&gt;' like it should, but '&' produces an error instead. It works if you convert the string before passing to AddChild()
Import BaH.libxml

Local XmlDoc:TxmlDoc = TxmlDoc.NewDoc("1.0")
Local Node:TxmlNode = TxmlNode.Newnode("story")
XmlDoc.SetRootElement(Node)
Node.AddChild("code1",Null,"<1>")
Node.AddChild("code2",Null,"<2>")
Node.AddChild("code3",Null,"<1&2>".Replace("&","&amp;")) 'replaces '&' with '&amp;'
XmlDoc.SaveFormatFile("test.xml",True,"utf-8")


Reading an xml doc works correctly, converting '&amp;' to '&' as expected.


Brucey(Posted 2013) [#2]
According to the docs for addChild()… ;-)

@content is supposed to be a piece of XML CDATA, so it allows entity references. XML special chars must be escaped first by using doc.#encodeEntities, or #addTextChild should be used.


You may want to use addTextChild() instead.


TomToad(Posted 2013) [#3]
Thanks, that worked. Odd how the characters '<' and '>' were being escaped with addChild(), but '&' wasn't.