如何在java中将多个xml合并为单个xml

时间:2010-02-03 16:12:45

标签: java xml

考虑我有2xml文件具有相同的结构没有dtd

xml1:

<DigestType name="Blogs">
<From>xxx@xx.org</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>xxx@xx.org</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>

现在我想将这两个xml合并为一个像

<DigestType name="Blogs">
<From>xxx@xx.org</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>

我正在使用java。任何人都可以帮我找出解决方案。

由于 UMA

3 个答案:

答案 0 :(得分:2)

使用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));

(您的XML格式不正确;我认为元素的开头是正确的名称。)

答案 1 :(得分:0)

最简单的方法可能是将每个文件加载到DOM文档中,然后从doc2中获取相应的子项并将它们添加到doc1,然后写出doc1。

答案 2 :(得分:0)

虽然XPath是一个很好的选择,但如果您的XML文档不是很大,我会推荐JDom,因为它会将整个文档加载到内存中。

使用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();
}

现在合并包含合并的文档。您可以使用XMLOutputter将其序列化为文件。