私の既存の XML (test.xml) を考えると:
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
</element>
</root>
そして私のルビーコード:
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)
私の問題は、出力を次のようにフォーマットすることです。
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
<child id="4" /></element>
</root>
...その後の追加は次のようになります。
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
<child id="4" /><child id="5" /><child id="6" /></element>
</root>
私が欲しいのはこれです:
<root>
<element>
<child id="1" />
<child id="2" />
<child id="3" />
<child id="4" />
<child id="5" />
<child id="6" />
</element>
</root>
…でもどうやって手に入れるの?