Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8070235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:21:04+00:00 2026-06-05T13:21:04+00:00

I have two XML files. The first XML has a bunch of nodes that

  • 0

I have two XML files. The first XML has a bunch of nodes that should be present in second XML as well. The second XML might have a few extra nodes as well. I need a Java based program that can automate this check – i.e. it should tell me that given two XML files, all the nodes of the first file is present in the second xml.

I am looking at Java + XMLUnit. However XMLUnit does not have a exact solution for this. Help please.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:21:06+00:00Added an answer on June 5, 2026 at 1:21 pm

    First things first. Let me go on record and say that XMLUnit is a gem. I loved it. If you are looking at some unit testing of XML values / attributes / structure etc. chances are that you will find a readymade solution with XMLUnit. This is a good place to start from.

    It is quite extensible. It already comes with an identity check (as in the XMLs have the same elements and attributes in the same order) or similarity check (as in the XMLs have the same elements and attributes regardless of the order).

    However, in my case I was looking for a slightly different usage. I had a big-ish XML (a few hundred nodes), and a bunch of XML files (around 350,000 of them). I needed to not compare certain particular nodes, that I could identify with XPATH. They were not necessarily always in the same position in the XML but there were some generic way of identifying them with XPATH. Sometimes, some nodes were to be ignored based on values of some other nodes. Just to give some idea

    1. The logic here is on the node that I want to ignore i.e price.
      /bookstore/book[price>35]/price

    2. The logic here is on a node that is at a relative position. I want to ignore author based on the value of price. And these two are related by position.
      /bookstore/book[price=30]/./author

    After much tinkering around, I settled for a low tech solution. Before using XMLUnit to compare the files, I used XPATH to mask the values of the nodes that were to be ignored.

        public static int massageData(File xmlFile, Set<String> xpaths, String mask)
            throws JDOMException, IOException {
        logger.debug("Data massaging started for " + xmlFile.getAbsolutePath());
        int counter = 0;
    
        Document doc = (Document) new SAXBuilder().build(xmlFile
                .getAbsolutePath());
    
        for (String xpath : xpaths) {
            logger.debug(xpath);
            XPathExpression<Element> xpathInstance = XPathFactory.instance()
                    .compile(xpath, Filters.element());
            List<Element> elements = xpathInstance.evaluate(doc);
            // element = xpathInstance.evaluateFirst(doc);
            if (elements != null) {
                if (elements.size() > 1) {
                    logger.warn("Multiple matches were found for " + xpath
                            + " in " + xmlFile.getAbsolutePath()
                            + ". This could be a *potential* error.");
                }
                for (Element element : elements) {
                    logger.debug(element.getText());
                    element.setText(mask);
                    counter++;
                }
            }
        }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two XML files. First XML File: <?xml version=1.0?> <AccessRequest xml:lang=en-US> <AccessLicenseNumber>Your_License</AccessLicenseNumber>
I am have two xml files.. I first get one and loop through it
I have two XML files that are generated by another application I have no
I have two XML files. They are similar, but there are two nodes of
I have two XML files. The structure of both XML files is as below:
this is my situation: I have two org.w3c.dom.Document created from two xml files. What
I have an XML file that is approximately 12mb which has about 16000 product's.
I have two persistence.xml files, for the sake of testing: src/main/resources/META-INF/persistence.xml src/test/resources/META-INF/persistence.xml How to
I want to parse a large XML file and I have two options: Perl
In this the catalog.xml file. I have two books who have the same inventory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.