首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JAVA合并多个XML文件的不同节点

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

Stack Overflow用户
提问于 2013-10-11 11:01:47
回答 3查看 5.5K关注 0票数 4

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

xml:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>

以及产出:

代码语言:javascript
复制
<?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实现这一点?抱歉,太长了!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-11 12:05:10

与其他可用的XML相比,拥有DOMBuilderSAXBuilder JDOM更适合:

  • 修改XML文档
  • XML树遍历和任意访问任意节
  • 合并文档

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

代码语言:javascript
复制
    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 
票数 6
EN

Stack Overflow用户

发布于 2013-10-11 11:15:53

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

您将在java中获得大量关于阅读写作 xml的示例。

票数 1
EN

Stack Overflow用户

发布于 2013-10-11 12:19:06

在我编写这里或以任何其他方式编写javax.xml.parsers.DocumentBuilder时,请解析XML,然后使用这里中的javax.xml.parsers.DocumentBuilder来完成您want.Create新的XML。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19316556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档