Hi Ace,
I see what you mean... so I had a rummage through the Libxml mailing list archives and found a post by the Libxml author who recommends that the "new" reader api is used to load files as it gives you more control of how it is handled via "options" params.
So, in good ole Blue Peter fashion... here's one I prepared earlier (or rather, moments ago)...
SuperStrict
Framework BaH.Libxml
' Create and save a document...
Local doc:TxmlDoc = TxmlDoc.newDoc("1.0")
Local rootNode:TxmlNode = TxmlNode.newNode("AllScores")
doc.setRootElement(rootNode)
rootNode.addChild("GlobalScores", Null, Null)
rootNode.addChild("LevelScores", Null, Null)
doc.saveFormatFile("indent_test.xml", True)
doc.free()
doc = Null
' Load the doc using the Reader API...
Local reader:TxmlTextReader = TxmlTextReader.fromFile("indent_test.xml", Null, XML_PARSE_NOBLANKS)
reader.read()
doc = reader.currentDoc()
' free up reader
reader.free()
reader = Null
' add some stuff
Local Node:TxmlNode = TxmlNode(doc.getRootElement().getFirstChild()).addChild("Level1")
Node.addAttribute("name", "brucey")
Node.addAttribute("score", "2")
' show doc
doc.saveFormatFile("-", True)
The key here is in the option passed into the TxmlTextReader fromFile() function : XML_PARSE_NOBLANKS
The issue arises because those indents you see in the saved xml are interpreted by Libxml on loading as "Text Nodes" (as they should be), and by default Libxml leaves *all* text nodes alone (well, you might want a node that has only spaces preserved for some reason!), and hence after you modify the xml, it can't then re-arrange the nodes to be indented - because of those text nodes.
The option XML_PARSE_NOBLANKS causes Libxml to strip out "empty/blank" text nodes so that when it comes to saving it with formatting, it can do so (by adding those nice spaces).
As the author says, it's up to the application-use to decide whether or not you want to strip these things out or not.
Anyways, hope that helps ;-)