Compare two XML attributes with same name in two different XML files using Java

958 views Asked by At

I have an XML with attributes and elements/tags. I want to know whether using an attribute or a tag is good according to performance.

Could you please give an example to compare if the content has a child tag and also if the content has a attribute. My question is, is it possible to compare 2 attributes with same name in 2 different XML files and also here we will have huge data. So, I want to be sure how the performance is, if i consider it as a attribute or tag.

    <A Name="HRMS">
    <B BName="IN">
    <C Code="0001">
      <IN irec="200" />
      <OUT orec="230" Number="" Outname=""/>
    </C>
    <C Code="0004">
      <IN irec="209" />
      <OUT orec="209" Number="" Outname=""/>
    </C>
    <C Code="0008">
      <IN irec="250" />
      <OUT orec="250" Number="" Outname=""/>
    </C>
    </B>
</A>

Here, i have to compare irec with orec for a particular B name and C code

1

There are 1 answers

1
Peter Pan On

It's possible. You need a java lib like jsoup to help parse xml by path expression like jquery css selection expression.

Jsoup is a HTML parser, but html is a kind of xml application, so you can use it to parse xml content.

jsoup example:

String xml = "<root><person name=\"Bob\"><age>20</age></person></root>";
Document root = Jsoup.parse(xml);
System.out.println(root.body().html());//origin XML content
Elements persons = root.getElementsByTag("person");
Element person = persons.first();
System.out.println("The attribute 'name' of Person:" + person.attr("name"));
System.out.println(persons.select("person[name=Bob]").first().text());

You can implement compare difference function using jsoup simplily.