将节点附加到现有的xml-Java [英] Append node to an existing xml-Java

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

问题描述

我已经看到了针对vb和c#的相同问题,但是我需要一个Java最佳解决方案来将节点附加到xml。请问xpath有帮助吗?
我有

I have seen the same question being answered for vb and c#, but i need a Java best solution for appending nodes to an xml. Will xpath help? I have

<A>
  <B>
    <c>1<c/>
    <d>2<d/>
    <e>3<e/>
  </B>
  <B>
    <c>1<c/>
    <d>2<d/>
    <e>3<e/>
  </B>
</A>

需要追加另一个

<B>
   <c>11<c/>
   <d>21<d/>
   <e>31<e/>
</B>


推荐答案

XPath将帮助您查找节点,但不是追加它们。我不认为你在这里发现它特别有用。

XPath will help you to find nodes, but not really append them. I don't think you'd find it particularly useful here.

你使用的是哪种XML API?如果它是W3C DOM(urgh)那么你会做类似的事情:

Which XML API are you using? If it's the W3C DOM (urgh) then you'd do something like:

Element newB = document.createElement("B");
Element newC = document.createElement("c");
newC.setTextContent("11");
Element newD = document.createElement("d");
newD.setTextContent("21");
Element newE = document.createElement("e");
newE.setTextContent("31");
newB.appendChild(newC);
newB.appendChild(newD);
newB.appendChild(newE);
document.getDocumentElement().appendChild(newB);

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

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