简体   繁体   English

在xml中使用dom4j在指定的位置添加新节点

[英]adding new node at specified place using dom4j in xml

My code to insert new cred-pair at desired machine level. 我的代码在所需的机器级别插入新的信用对。

Document insertNewNode(String loginId,String pass,String machine_name)
{
List<?> list1 = document.selectNodes("//machine/@name" );
Iterator<?> itr=list1.iterator();
while(itr.hasNext()){
Attribute attribute=(Attribute)itr.next();
if( attribute.getValue().equals(machine_name))
{
    List<?> list1 = document.selectNodes("//machine" );
    Iterator<?> iter=list.iterator();
    while(iter.hasNext()){
    Element credPairs=(Element)iter.next();
    Element credPair =credPairs.addElement("cred-pair");
    Element login =credPair.addElement("login");
    element.setText(loginId);
    Element password =credPair.addElement("password");
    element.setText(pass);

}

}
}
}

Original xml: 原始xml:

<credentials>
<machine name="xyz">
<cred-pairs>
<cred-pair>
<login>asad</login>
<password>12345</password>
</cred-pair>
<cred-pairs>
</machine>
<machine name="pqr">
<cred-pair>
<cred-pair>
<login>ssdas</login>
<password>12345</password>
</cred-pair>
<cred-pairs>
</machine>
</credentials>

If I call insertNewNode(ggss,97653,xyz) 如果我打电话给insertNewNode(ggss,97653,xyz)

Expected xml: 预期的xml:

<credentials>
 <machine name="xyz">
  <cred-pairs>
   <cred-pair>
    <login>asad</login>
    <password>12345</password>
   </cred-pair>
 **<cred-pair>
    <login>ggss</login>
    <password>97653</password>
   </cred-pair>**
   <cred-pairs>
 </machine>
 <machine name="pqr">
  <cred-pair>
   <cred-pair>
    <login>ssdas</login>
    <password>12345</password>
   </cred-pair>
   <cred-pairs>
 </machine>
</credentials>

But I am getting output as: 但是我得到的输出为:

<credentials>
 <machine name="xyz">
  <cred-pairs>
   <cred-pair>
    <login>asad</login>
    <password>12345</password>
   </cred-pair>
** <cred-pair>
    <login>ggss</login>
    <password>97653</password>
   </cred-pair>**
  <cred-pairs>
 </machine>
 <machine name="pqr">
  <cred-pair>
   <cred-pair>
    <login>ssdas</login>
    <password>12345</password>
   </cred-pair>
 **<cred-pair>
    <login>ggss</login>
    <password>97653</password>
   </cred-pair>
  <cred-pairs>**
 </machine>
</credentials>

After indentation I saw, that you xml is not well-formatted. 缩进后,我看到您的xml格式不正确。 There is one <cred-pairs> opening tag right before </machine> and that's illegal at that place. </machine>前面有一个<cred-pairs>开头标签,在那个地方是非法的。 May be a copy/paste error. 可能是复制/粘贴错误。

After you've found the correct machine name attribute you select all machine nodes and add the credentials to every machine node. 找到正确的计算机名称属性后,请选择所有计算机节点并将凭据添加到每个计算机节点。 Instead you shouldn't select attributes but the elements that qualify for the machine name: 相反,您不应选择属性,而应选择符合计算机名称的元素:

List<Element> machines = document.selectNodes(String.format("//machine[@name='%s']", machine_name);
for (Element machine:machines) {
    Element credPairs=(Element)iter.next();
    Element credPair =credPairs.addElement("cred-pair");
    Element login =credPair.addElement("login");
    login.setText(loginId);
    Element password =credPair.addElement("password");
    password.setText(pass);
    machine.addElement(credPairs);    
}

Untested but should work (or show you the direction) 未经测试,但应该可以(或向您显示方向)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM