Tuesday, 6 August 2013

Modifying tags of an XML

Modifying tags of an XML

I'm looking for the best way to dynamically modify the tags of a very
large XML file.
Consider the following input XML:
Input
<?xml version="1.0" encoding="UTF-8"?>
<rootTag>
<dictionary>
<name>field1</name>
<address>field2</address>
<gender>field3</gender>
.
.
<postcode>field30</postcode>
</dictionary>
<records>
<record>
<field id="field1">John</field>
<field id="field2">Svalbard</field>
<field id="field3">M</field>
.
.
<field id="field30">12345</field>
</record>
.
.
<record>
.
.
</record>
</records>
</rootTag>
The XML file contains a dictionary on top and a huge chunk of record
nodes, whose tags are linked to the dictionary.
I'd like to replace the tags within each record node to their
corresponding value from the dictionary. Thus, the output should look
like:
Output
<?xml version="1.0" encoding="UTF-8"?>
<rootTag>
<records>
<record>
<name>John</name>
<address>Svalbard</address>
<gender>M</gender>
.
.
<postcode>12345</postcode>
</record>
.
.
<record>
.
.
</record>
</records>
</rootTag>
Keeping in mind that there are a tremendously large number of <record>
nodes, what's the best way to achieve this transformation in Java?
Note that I only want to change the tags and not the attributes.

No comments:

Post a Comment