All of the interesting technological, artistic or just plain fun subjects I'd investigate if I had an infinite number of lifetimes. In other words, a dumping ground...

Thursday 30 April 2009

libxml2 indenting or formatting output

I spent ages trying to get libxml2 and xmllint to format the output of an XML file.
The xml file was:
<?xml version="1.0"?>
<root>content<child1>child1 content<child2>child2 content</child2><child3>child3 content</child3><child4>child4 content<child5>child5 content</child5></child4><child6>child6 content</child6></child1></root>

Here you notice that the nodes that have child nodes also have content - I didn't know this was possible in xml. I guess that's why Daniel Villiard harps on how whitespace is significant.
Anyway I removed the content from the nodes that had children and low and behold:

<?xml version="1.0"?>
<root>
  <child1>
    <child2>child2 content</child2>
    <child3>child3 content</child3>
    <child4>
      <child5>child5 content</child5>
    </child4>
    <child6>child6 content</child6>
  </child1>
</root>

Works like a dream...

No comments:

tim's shared items

Add to Google Reader or Homepage