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

使用JAVA合并多个XML文件的不同节点,java,xml,merge,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"

我在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>

布尔A,B;
布尔C;
制度与制度;
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>

int f,k;
布尔D;
系统或系统;
以及输出:

<?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>

布尔A,B;
布尔C;
int f,k;
布尔D;
系统和/或;

基本上,我希望将
声明
系统
合并,其余部分在输出xml文件中串行。如何使用JAVA实现这一点?抱歉发了这么长的帖子

您可以使用xml解析器,例如读取所需的部分(声明和系统),然后聚合它们以获得所需的输出


您将获得大量关于java和xml的示例。

与其他可用的xml处理API相比,对我来说, 拥有
DOMBuilder
SAXBuilder
更适合:

  • 修改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 
在编写时解析XML,或者以任何其他方式解析,然后从中使用javax.XML.parsers.DocumentBuilder
创建一个新的XML文档。

看看如何使用
JDOM
这应该是小菜一碟。非常感谢您。这个很好用。但我还有一个问题。如果第一个XML中有三个不同的
模板
,第二个XML中有五个,我想保留它们的结构,那该怎么办。我是怎么做到的?对循环使用
?是。对
循环使用
。试试看。然而,使用该网站的常见方式是,如果你认为答案解决了你的问题,就标记一个被接受的答案。哦,对不起,我不知道。我认为绿色箭头会随着投票上升,我不能投票,直到我的名声恢复到15岁,一切正常。你不必道歉。我只是想让你知道。许多用户回避新用户的问题,因为有时他们会忘记向上投票。谢谢