Search code examples
javaxmljspxml-parsingjavabeans

How to Parse XML Document By Class Name Using Java


I'm writing a parsing tool to compare the textual content of two bean XML files in Java. The text content changes and we need a way to run a script to make sure the textual content is the same. I know we have org.w3c.dom which has a method getElementsByTagName("tag_name") and that returns a node list in the XML document. I'm wondering if anyone knows of a way to do this using the class name? I've been searching around but haven't been able to solve this yet.

<bean class="com.mycompany.myText" id="Q1.4">
    <property name="name">
        <value>Q1.4</value>
    </property>
    <property name="text">
        <value>This is text one</value>
    </property>
    <property name="retired">
            <value>true</value>
    </property>
</bean>

<bean class="com.mycompany.myText" id="Q1.5">
    <property name="name">
        <value>Q1.5</value>
    </property>
    <property name="text">
        <value>This is text two</value>
    </property>
    <property name="retired">
            <value>true</value>
    </property>
</bean>

I can't use the 'bean' element name as there are several other beans whith non relevant stuff I need only the ones with the class com.mycompany.myText and the value I'm trying to extract is property name=text - the text content

Any help here would be appreciated. Also as a side note I should mention that we have no direct control of how the XML file is managed and structured as it's fed to us from a 3rd party.


Solution

  • As you say, you need to select tags by their attributes.

    You could use XPath to achieve this. See those resources:

    The expression would be something like this

    /bean[@class='com.mycompany.myText']/property[@name='text']/value

    I hope I could help at least a little bit.