ziggy
ziggy

Reputation: 15876

Best practice to parse xml file with same content but based on different schemas

I need to parse xml files from two sources. Both xml files contain the same content but each source uses their own schemas. This means the values that i want to extract from the xml file will be stored in different element names depending on the source of the file.

Here is an example - Assume i am only interested in the "name" of a product.

Source 1
-------------------------
    <item>
     <itemname>Camera</itemname>
     <itemprice>20</itemprice>
    </item>

Source 2
-------------------------
    <productList>
     <productName>Camera</productname>
     <ProductPrice>20</productprice>
    </productList>

To parse the above i have to know the source of the xml file and then either do a

getElementsByTagName("itemname");

or

getElementsByTagName("productName");

My original plan was to have a different parser for each source's xml file but i am thinking that maybe i could write a generic parser if i specify the path to the element i need. The benefit of this is that i can then process any xml file from any source without having to modify the parser.

What i am thinking of doing is to store the path to the element on to a properties file. i.e.

source1.name="itemname"
source2.name=productName

The generic parser would then just retrieve the element based on the name i provide it. This will probably work but i am thinking that if i am interested in more than one element it might be cumbersome to maintain it via a properties file.

Is there a better way to resolve the above? Please note that One restriction that i am limited to is that the target platform for this is JDK 1.4 so xpath etc would not work.

Upvotes: 0

Views: 1495

Answers (2)

agranjo
agranjo

Reputation: 1374

If element names follow a convention (*Name, *Price), you could write a generic parsing function using wildcards and XPath. Or you could write it based on tag orders if they are always the same (you can do this without XPath).

Upvotes: 0

Mark Thomas
Mark Thomas

Reputation: 37517

The ideal solution is XPath. No matter how different the XML inputs are, you can store an XPath for each as a string in a properties file. There are several XPath-compliant parsers that work with JDK 1.4.

Upvotes: 1

Related Questions