【问题标题】:Merging different nodes of multiple XML files using JAVA使用JAVA合并多个XML文件的不同节点
【发布时间】:2013-10-19 10:34:03
【问题描述】:

我在 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 做到这一点?抱歉发了这么长的帖子!!!

【问题讨论】:

  • 看看this
  • 使用JDOM它应该是小菜一碟。

标签: java xml merge


【解决方案1】:

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

您将在 java 中的 readingwriting xml 中获得大量示例。

【讨论】:

    【解决方案2】:

    与其他可用的 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 
    

    【讨论】:

    • 非常感谢您。这工作正常。但我还有一个问题。如果第一个 XML 中有三个不同的template,第二个 XML 中有五个,我想保留它们的结构怎么办。我该怎么做?使用for 循环?
    • 是的。使用for 循环。试一试。但是,使用该网站的常见方式是,如果您认为答案解决了您的问题,则将其标记为已接受。
    • 哦,对不起,我不知道。我认为绿色箭头与投票一致,直到我的声望达到 15 岁才能投票
    • 没关系。你不必感到抱歉。我只是想让你知道。许多用户回避新用户的问题,因为有时他们忘记了投票。谢谢
    【解决方案3】:

    在我编写 here 时解析 XML,或者以任何其他方式解析 XML,然后使用来自 here 的 javax.xml.parsers.DocumentBuilder 做你想做的事。创建一个新的 XML 文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多