java - 使用JAVA合并多个XML文件的不同节点

标签 java xml merge

我在 arrayList 中有一些 xml 文件,例如 A.xml B.xml 我想合并一些节点,而其余的节点保持原样使用 java。我刚开始使用,所以我不知道该怎么做。

一个 xml:

<?xml version="1.0" encoding="UTF-8"?>
<nta>
<declaration>
    bool A, B;
    bool C;
</declaration>
<template>
    <location id="1"  x="10" y="10"/>
    <transition>
        <source ref="3"/>
    </transition>    
</template>
<system> system AND;</system>
</nta>

B.xml:

<?xml version="1.0" encoding="UTF-8"?>
<nta>
<declaration>
    int f,k;
    bool D;
</declaration>
<template>
    <location id="100"  x="40" y="89"/>
    <transition>
        <source col="9"/>
    </transition>    
</template>
<system> system OR;</system>
</nta>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<nta>
<declaration>
    bool A, B;
    bool C;
    int f,k;
    bool D;
</declaration>
<template>
    <location id="1"  x="10" y="10"/>
    <transition>
        <source ref="3"/>
    </transition>    
</template>
<template>
    <location id="100"  x="40" y="89"/>
    <transition>
        <source col="9"/>
    </transition>    
</template>
<system> system AND, OR;</system>
</nta>

基本上我想合并 declarationsystem 以及输出 xml 文件中的其余部分。如何使用 JAVA 做到这一点?抱歉发了这么长的帖子!!!

最佳答案

与其他可用的 XML 处理 API 相比,对我来说, 拥有DOMBuilderSAXBuilder JDOM更适合:

  • 修改 XML 文档
  • XML 树遍历和随机访问任何部分
  • 合并文件

这是合并两个 XML 文档的完整工作示例:

    SAXBuilder builder = new SAXBuilder();
    Document doc1 = builder.build(new File("E:\\XML1.xml"));
    Document doc2 = builder.build(new File("E:\\XML2.xml"));

    String rootName = doc1.getRootElement().getName();
    Element newRoot = new Element(rootName);
    Document newDoc = new Document(newRoot);

    Element root1 = doc1.getRootElement();
    Element root2 = doc2.getRootElement();

         // creating declaraion element by merging the declaration content
    Element declaration = new Element("declaration");
    declaration.addContent(root1.getChildText("declaration"));
    declaration.addContent(root2.getChildText("declaration"));
    newRoot.addContent(declaration); // add declaration element to new document

         newRoot.addContent(root1.getChild("template").clone()); 
                       // directly adding template from document XML1, 
                      //after getting template child,
                     //it needs to be cloned to detached  from its parent  

     newRoot.addContent(root2.getChild("template").clone()); 
                       // same for document XML2

     /*** now code yourself  for system element here ***/

    XMLOutputter outputter = new XMLOutputter();
    outputter.output(newDoc, System.out); 
                  // output the new doc, pass your OutputStream to this function 

关于java - 使用JAVA合并多个XML文件的不同节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19316556/

相关文章:

java - 如何在Android中设置自定义不同的字体样式

xml - 将副本传递给函数?

java - Camel XML路由: can I give a "uri" attribute in a <from> element the value of a Java constant?

PHP 根据给定索引的匹配值合并数组

linux - 基于两列合并两个文件

java - 如何使用 Selenium 和 Java 选择第一个自动建议

java - 如何在两个 Action 之间添加延迟

java - 尝试访问注入(inject)的 Bean 时出现空指针异常

java - 检测对象是否为 List<String>

vba - 将多个 .xls 文件合并到一张表中