2

In Java:
Suppose I have 3 xml files

<student>lin</student> --  file1.xml

<student>Eric</student> --  file2.xml

<student>joe</student> --  file3.xml

How can I merge these xml’s (considering that they don’t have the DTD or namespace declaration) to create

<class><student>lin</student> <student>Eric</student>
<student>joe</student> </class> -- file4.xml

class being the wrapping node I supply manually

Ps: I used xstream to create the xml’s

3
  • Is <class> a top level element for the whole XML document, or an existing xstream object? Commented Jan 18, 2010 at 17:01
  • @Kaleb top level element Commented Jan 18, 2010 at 17:02
  • There is fairly straightforward solutionunless u r seeking a general solution,use XMLNOde objects which point to the Student node in first xml and append them to node in seconf xmlDocument object. Commented Jan 29, 2010 at 14:59

3 Answers 3

2

I your files are large I would use a SAXParser where your ContentHandler would echo the tags and the content.

Something like (pseudo-code):

print("<class>")
foreach(file in files)
  {
  mysaxparser.parse(new Handler() 
     {
     content="";

     void endElement(tag)
         {
          if(tag.equals("student")) print("<student>"+escapeXML(content)+"</student>"); 
         content="";
         }
     void characters(str)
         {
         content+=str;
         }
     },file);
  }
print("</class>");

If your files are small enough to fit into the memory: load the DOM of each document using a DocumentBuilder and call importNode to merge the documents into one.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm tempted to give you -1 for that hacky SAX solution, but your DOM solution is all but guaranteed to be the best approach.
This looks out of the way from OOP concept.
1

If you know the files are each well-formed you could concatenate them together (after removing any prolog entry from them), along with a header and footer containing the root element start and end tags.

String[] filenames = new String[]{"header.xml", "file1.xml", "file2.xml", "file3.xml", "footer.xml"};
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("merged.xml");
for (String filename : filenames) {
    InputStream inputStream = new BufferedInputStream(new FileInputStream(filename);
    org.apache.commons.io.IOUtils.copy(inputStream, outputStream);
    inputStream.close();
}
outputStream.close();

1 Comment

This will work as long as the individual files don't have a prologue.
1

I think the correct way to do it is to load the three files into DOM documents and then have one of them adopt the nodes from the other two documents, this way it is all handled by the dom api and sould be foolproof, instead of text manipulation.

You can achieve this by looking into the DomDocument javadoc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.