How can i merge multiple xml into single xml in java -


Consider that I have 2xml files that are of the same framework, not

xml1 :

  & lt; Digest Type Name = "Blog" & gt; & Lt; To & gt; Xxx@xx.org< / FromEmail & gt; & Lt; EmailTimeStamp & gt; & Lt; / EmailTimeStamp & gt; & Lt; EmailSubject & gt; & Lt; / EmailSubject & gt; & Lt; BlogName href = "" & gt; XML merger & lt; / Blogname & gt; & Lt; Blog calculation = "2" & gt; & Lt; ListPost & gt; & Lt; Title href = "" & gt; Title & lt; / Heading & gt; & Lt; CommentCount & gt; 10 & lt; / CommentCount & gt; & Lt; Content & gt; Post content ... & lt; / Content & gt; & Lt; / ListBlogPost & gt; & Lt; ListPost & gt; & Lt; Title href = "" & gt; Title 1 & lt; / Heading & gt; & Lt; CommentCount & gt; 10 & lt; / CommentCount & gt; & Lt; Content & gt; Post content ... & lt; / Content & gt; & Lt; / ListBlogPost & gt; & Lt; / Blog & gt; & Lt; / DigestType & gt;  

xml2:

  & lt; Digest Type Name = "Blog" & gt; & Lt; To & gt; Xxx@xx.org< / FromEmail & gt; & Lt; EmailTimeStamp & gt; & Lt; / EmailTimeStamp & gt; & Lt; EmailSubject & gt; & Lt; / EmailSubject & gt; & Lt; BlogName href = "" & gt; XML merger & lt; / Blogname & gt; & Lt; Blog calculation = "2" & gt; & Lt; ListPost & gt; & Lt; Title href = "" & gt; Title 2 & lt; / Heading & gt; & Lt; CommentCount & gt; 1 & lt; / CommentCount & gt; & Lt; Content & gt; Content ... & lt; / Content & gt; & Lt; / ListBlogPost & gt; & Lt; ListPost & gt; & Lt; Title href = "" & gt; Title 3 & lt; / Heading & gt; & Lt; CommentCount & gt; 23 & lt; / CommentCount & gt; & Lt; Content & gt; Content ... & lt; / Content & gt; & Lt; / ListBlogPost & gt; & Lt; / Blog & gt;  

Now I can add these 2 XML to

  & lt; DigestType name = "blogs" & gt; & Lt; To & gt; Xxx@xx.org< / FromEmail & gt; & Lt; EmailTimeStamp & gt; & Lt; / EmailTimeStamp & gt; & Lt; EmailSubject & gt; & Lt; / EmailSubject & gt; & Lt; BlogName href = "" & gt; XML merger & lt; / Blogname & gt; & Lt; Blog Count = "4" & gt; & Lt; ListPost & gt; & Lt; Title href = "" & gt; Title & lt; / Heading & gt; & Lt; CommentCount & gt; 10 & lt; / CommentCount & gt; & Lt; Content & gt; Post content ... & lt; / Content & gt; & Lt; / ListBlogPost & gt; & Lt; ListPost & gt; & Lt; Title href = "" & gt; Title 1 & lt; / Heading & gt; & Lt; CommentCount & gt; 10 & lt; / CommentCount & gt; & Lt; Content & gt; Post content ... & lt; / Content & gt; & Lt; / ListBlogPost & gt; & Lt; ListPost & gt; & Lt; Title href = "" & gt; Title 2 & lt; / Heading & gt; & Lt; CommentCount & gt; 1 & lt; / CommentCount & gt; & Lt; Content & gt; Content ... & lt; / Content & gt; & Lt; / ListBlogPost & gt; & Lt; ListPost & gt; & Lt; Title href = "" & gt; Title 3 & lt; / Heading & gt; & Lt; CommentCount & gt; 23 & lt; / CommentCount & gt; & Lt; Content & gt; Content ... & lt; / Content & gt; & Lt; / ListBlogPost & gt; & Lt; / Blog & gt; & Lt; / DigestType & gt;  

I am using Java Can someone help me find solutions?

Thanks uma

Use it:

  XPath xpath = XPathFactory.newInstance (). NewXPath (); // Load Document xml1 = (document) xpath.evaluate ("/", new input source ("xml1.xml"), XPathConstants.NODE); Nodelist list post = (nodelist) xpath.evaluate ("/ digest type / blog / list post", new input source ("xml2.xml"), XPathConstants.NODESET); // Merge element blog = (element) xpath.evaluate ("/ digest type / blog", xml1, XPathConstants.NODE); (Int i = 0; i & lt; list.posts.length (); i ++) {node list post = list post.system (i); Blog.appendChild (xml1.adoptNode (listPost)); } // Set blog.setAttribute ("count", xpath.evaluate ("count (list post)", blog)); // print DOMImplementationLS impl = (DOMImplementationLS) xml1.getImplementation (); Println (impl.createLSSerializer () writeToString (xml1).);  

(Your XML is not created properly, I think the element starts with the correct name.)

< / Html>

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -