tags:

views:

209

answers:

3

consider i have 2xml files haveing same structure dont have dtd

xml1 :

<DigestType name="Blogs">
<From>[email protected]</FromEmail>
<EmailTimeStamp></EmailTimeStamp>
<EmailSubject></EmailSubject>
<BlogName href="">Merging XML</BlogName>
<Blog Count="2">
<listPost>
    <Title href="">Title</Title>
    <CommentCount>10</CommentCount>
    <Content>Post content...</Content>
</listBlogPost>
<listPost>
    <Title href="">Title1</Title>
    <CommentCount>10</CommentCount>
    <Content>Post content...</Content>
</listBlogPost>
</Blog>
</DigestType>

xml2 :

<DigestType name="Blogs">
<From>[email protected]</FromEmail>
<EmailTimeStamp></EmailTimeStamp>
<EmailSubject></EmailSubject>
<BlogName href="">Merging XML</BlogName>
<Blog Count="2">
        <listPost>
    <Title href="">Title2</Title>
    <CommentCount>1</CommentCount>
    <Content>content...</Content>
</listBlogPost>
<listPost>
    <Title href="">Title3</Title>
    <CommentCount>23</CommentCount>
    <Content>content...</Content>
</listBlogPost>
</Blog>

now i want to merge these 2 xml as one like

<DigestType name="Blogs">
<From>[email protected]</FromEmail>
<EmailTimeStamp></EmailTimeStamp>
<EmailSubject></EmailSubject>
<BlogName href="">Merging XML</BlogName>
<Blog Count="4">
     <listPost>
    <Title href="">Title</Title>
    <CommentCount>10</CommentCount>
    <Content>Post content...</Content>
</listBlogPost>
<listPost>
    <Title href="">Title1</Title>
    <CommentCount>10</CommentCount>
    <Content>Post content...</Content>
</listBlogPost>


<listPost>
    <Title href="">Title2</Title>
    <CommentCount>1</CommentCount>
    <Content>content...</Content>
</listBlogPost>
<listPost>
    <Title href="">Title3</Title>
    <CommentCount>23</CommentCount>
    <Content>content...</Content>
</listBlogPost>
</Blog>
</DigestType>

Im using java. can anyone help me to find out the solution.

Thanks uma

A: 

Easiest way might be to load each file into a DOM document, then get the appropriate children from doc2 and add them to doc1, then write out doc1.

shoover
+1  A: 

Using XPath:

XPath xpath = XPathFactory.newInstance().newXPath();
// load
Document xml1 = (Document) xpath.evaluate("/", new InputSource("xml1.xml"),
    XPathConstants.NODE);
NodeList listPosts = (NodeList) xpath.evaluate("/DigestType/Blog/listPost",
    new InputSource("xml2.xml"), XPathConstants.NODESET);
// merge
Element blog = (Element) xpath.evaluate("/DigestType/Blog", xml1,
    XPathConstants.NODE);
for (int i = 0; i < listPosts.getLength(); i++) {
  Node listPost = listPosts.item(i);
  blog.appendChild(xml1.adoptNode(listPost));
}
// set count
blog.setAttribute("Count", xpath.evaluate("count(listPost)", blog));
// print
DOMImplementationLS impl = (DOMImplementationLS) xml1.getImplementation();
System.out.println(impl.createLSSerializer().writeToString(xml1));

(Your XML is not well formed; I assumed the element starts were the correct names.)

McDowell
A: 

While XPath is a great choice, I would recommend JDom if your XML document is not huge, since it loads the entire document in memory.

Using JDom:

try {
    SAXBuilder builder = new SAXBuilder();
    Document firstDocument = builder.build(firstFile);
    Document secondDocument = builder.build(secondFile);

    Element firstRoot = firstDocument.getRootElement();
    Element secondRoot = secondDocument.getRootElement();

    List<Element> sourceListPost = secondRoot.getChild("listPost");
    firstRoot.addContent(sourceListPost);

    Document merged = new Document(firstRoot);
} catch(JDOMException e) {
    e.printStackTrace();
} catch(IOException e) {
    e.printStackTrace();
}

Now merged contains the merged Document. You can serialize it to a file with XMLOutputter.

Cesar