0

First xml file test_balance1.xml enter image description here

Second xml file test_balance2.xml enter image description here

How to parse two xml file using SAX parser and compare specific elements in java?

Ex: i need to parse

                line 12: <brm:CURRENT_BAL>-30</brm:CURRENT_BAL> 
                line 24: <brm:CURRENT_BAL>0</brm:CURRENT_BAL>  of test_balance1.xml and

                line 12: <brm:CURRENT_BAL>55</brm:CURRENT_BAL> 
                line 24: <brm:CURRENT_BAL>20</brm:CURRENT_BAL> of test_balance2.xml

After parsing How to compare the specific elements, i.e. CURRENT_BAL= -30 and CURRENT_BAL=55 , also CURRENT_BAL=0 and CURRENT_BAL=20

1 Answers1

0

You can use XMLUtils + refer answers here : Best way to compare 2 XML documents in Java

import oracle.xml.diff.XmlUtils;
import oracle.xml.diff.Options;

import java.io.File;

import org.w3c.dom.Node;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

public class textDiff
{
    public static void main(String[] args) throws Exception
    {
        XmlUtils xmlUtils = new XmlUtils();

        //Parse the two input files
        DocumentBuilderFactory dbFactory =   
                  DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        DocumentBuilder docBuilder = 
                  dbFactory.newDocumentBuilder();
        Node doc = docBuilder.parse(new File(args[0]));
        Node doc1 = docBuilder.parse(new File(args[1]));

        //Run the diff
        try
        {
            Document diffAsDom = xmlUtils.diffToDoc(doc, 
                                  doc1, new Options());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
Community
  • 1
  • 1
Maas
  • 1,317
  • 9
  • 20