Given my existing XML (test.xml):
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
</element>
</root>
And my ruby code:
require 'rubygems'
require 'xml'
parser = XML::Parser.file("test.xml")
doc = parser.parse
target = doc.find('/*/element')
target << child = XML::Node.new('child')
child['id'] = '4'
XML.indent_tree_output = true
doc.save(filename, :indent => true, :encoding => XML::Encoding::UTF_8)
My problem is that it formats the output like this:
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
<child id="4" /></element>
</root>
... with subsequent additions looking like this:
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
<child id="4" /><child id="5" /><child id="6" /></element>
</root>
What I WANT is this:
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
<child id="4" />
<child id="5" />
<child id="6" />
</element>
</root>
... but how do I get it?