如何将节点从xml文档附加到现有的xml文档 [英] how to append node from xml document to existing xml document

查看:99
本文介绍了如何将节点从xml文档附加到现有的xml文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的a.xml中有锦标赛列表:

 < tournaments> 

< tournament>
< name> a< / name>
< / tournament>

< tournament>
< name> b< / name>
< / tournament>

< tournament>
< name> c< / name>
< / tournament>

< / tournaments>

广告然后我在b.xml中有一个比赛

 < tournament> 
< name> d< / name>
< / tournament>

如何将文件b.xml加载到a.xml作为另一场比赛?



所以这就是我想要的:

 < tournaments> 

< tournament>
< name> a< / name>
< / tournament>

< tournament>
< name> b< / name>
< / tournament>

< tournament>
< name> c< / name>
< / tournament>

< tournament>
< name> d< / name>
< / tournament>

< / tournaments>


解决方案


  1. 获取节点从第一个添加文档;

  2. 采用节点(参见文档.adopt(Node))从第一个文档到第二个文档;

  3. Appent采用 Node 作为一个小孩第二文档结构(请参阅节点。 appendChild(Node)

更新。
代码:

  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();

文件比赛= builder.parse(new File(b.xml));
文件比赛= builder.parse(new File(a.xml));

节点tournamentElement = tournament.getFirstChild();
Node ndetournament = tournaments.getDocumentElement();
Node firstDocImportedNode = tournaments.adoptNode(tournamentElement);
ndetournament.appendChild(firstDocImportedNode);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
变压器变压器= transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,yes);
transformer.transform(new DOMSource(tournaments)),新的StreamResult(System.out));

结果:

 code><?xml version =1.0encoding =UTF-8standalone =否?> 
<锦标赛>
< tournament>
< name> a< / name>
< / tournament>

< tournament>
< name> b< / name>
< / tournament>

< tournament>
< name> c< / name>
< / tournament>
< tournament>
< name> d< / name>
< / tournament>
< / tournaments>


I have list of tournaments in my a.xml:

<tournaments>

    <tournament>
        <name>a</name>
    </tournament>

    <tournament>
        <name>b</name>
    </tournament>

    <tournament>
        <name>c</name>
    </tournament>

</tournaments>

ad then I have one tournament in b.xml

<tournament>
    <name>d</name>
</tournament>

How I can apend document b.xml to a.xml into as another tournament ?

so this is what I want:

<tournaments>

    <tournament>
        <name>a</name>
    </tournament>

    <tournament>
        <name>b</name>
    </tournament>

    <tournament>
        <name>c</name>
    </tournament>

    <tournament>
        <name>d</name>
    </tournament>

</tournaments>

解决方案

  1. Get Node to add from first Document;
  2. Adopt Node (see Document.adopt(Node)) from first Document to the second Document;
  3. Appent adopted Node as a child to second Document structure (see Node.appendChild(Node).

Update. Code:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();

Document tournament = builder.parse(new File("b.xml"));
Document tournaments = builder.parse(new File("a.xml"));

Node tournamentElement = tournament.getFirstChild();
Node ndetournament = tournaments.getDocumentElement();
Node firstDocImportedNode = tournaments.adoptNode(tournamentElement);
ndetournament.appendChild(firstDocImportedNode);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(tournaments), new StreamResult(System.out));

Result:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<tournaments>
    <tournament>
        <name>a</name>
    </tournament>

    <tournament>
        <name>b</name>
    </tournament>

    <tournament>
        <name>c</name>
    </tournament>
<tournament>
    <name>d</name>
</tournament>
</tournaments>

这篇关于如何将节点从xml文档附加到现有的xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆