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 9174779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:48:26+00:00 2026-06-17T16:48:26+00:00

Given a set of three XML files: first.xml <root> <item1>A</item1> <complexItem> <item2>B</item2> <item3>C</item2> </complexItem>

  • 0

Given a set of three XML files:

first.xml

<root>
    <item1>A</item1>
    <complexItem>
        <item2>B</item2>
        <item3>C</item2>
    </complexItem>
</root>

patch1.xml

<root>
     <item1>X</item1>
</root>

patch2.xml

<root>
     <complexItem>
         <item3>Y</item>
     </complexItem>
     <differentItem>Z</differentItem>
<root>

I would like to end up with the XML:

patched.xml

<root>
    <item1>X</item1>
    <complexItem>
        <item2>B</item2>
        <item3>Y</item2>
    </complexItem>
    <differentItem>Z</differentItem>
</root>

So new elements in the patches are additive and existing elements in patches are destructive. These additions and updates can take place at any level of the document tree. Ideally, this would be a maven plugin which can take a list of files as arguments, though a solution in Java (i.e. an available library – I’m trying to avoid reinventing something which seems like it should have already been done!) is fine as I can write the plugin myself. Each file (base and patches) will always have the same root element.


I should have added, there is no use case to remove elements from the tree (the hierarchical nature of our file replacements cause this to be an error case for the application using the patched files). I did a little more searching for a pre-built tool or library and couldn’t find anything suitable, so took Andrew’s advice of building something from scratch with dom4j. Quite unfamiliar with dom4j, but here’s roughly what I’ve come up with (without review/error handling/proper commenting etc.):

public void execute(){

    // Environment specific file loading removed

    SAXReader reader = new SAXReader();
Document patchedDocument = null;
for (InputStream is : loadedFiles) {
    Document d = null;
    try {
        d = reader.read(is);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    if (patchedDocument == null) {
        patchedDocument = d;
    } else {
        Element root = d.getRootElement();
        patch(patchedDocument, root);
    }
}

    // Environment specific file writing

}

private void patch(Document patchedDocument, Element element) {

    for (Iterator i = element.elementIterator(); i.hasNext();) {
        Element nextElement = (Element) i.next();
        if (nextElement.isTextOnly()) {
            String path = nextElement.getUniquePath();
            Node n = patchedDocument.selectSingleNode(path);
            if (n != null)
            {
                                // This already exists and needs content replacing    
                n.setText(nextElement.getText());
            }else{
                                // This doesn't exist and needs to be added to the tree
                addElement(patchedDocument, nextElement);
            }
        } else {
            patch(patchedDocument, nextElement);
        }
    }
}

private Node addElement(Document patchedDocument, Element element)
{
    Element parent = element.getParent();
    String parentPath = parent.getPath();
    Node n = patchedDocument.selectSingleNode(parentPath);
    if (n == null){
        return addElement(patchedDocument, parent);
    } else {
        ((Branch)n).add(element.detach());
        return n;
    }
}
  • 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-17T16:48:28+00:00Added an answer on June 17, 2026 at 4:48 pm

    If the files are small enough to fit in memory, I’d write some Java code to slurp them all into separate DOMs, then merge them into one DOM based on the additive/destructive logic you’ve described, then spit the merged DOM back out into the target file. There are several Java libraries that could be used for the reading and writing of the XML files, e.g. dom4j. You don’t want to be messing with trying to parse or construct the XML yourself.

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

Sidebar

Related Questions

I have a set of XML files that I am processing with an XSL
So, my first question, is LINQ to XML order dependent? Given the XML (simplified
Given the following XML section from a large xml file: <item id=C3DD6846593 > <name
Is there any way of automating a site publish with a given set of
Is there a way to know where is set a given PHP config property?
For any given NSManagedObject, is there any method to return a set or array
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
This is one of the solution of getting true or false from given set
Given a set of questions that have linked survey and category id: > db.questions.find().toArray();
Given a set of phrases, i would like to filter the set of all

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.