使用 xml 中的 dom4j 在指定位置添加新节点
我的代码用于在所需的机器级别插入新的信用对。
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);
}
}
}
}
原始 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>
如果我调用 insertNewNode(ggss,97653,xyz)
预期 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>
但我得到的输出为:
<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>
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:
<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)
Expected 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
缩进后我发现你的 xml 格式不正确。在
之前有一个
开始标记,这在该位置是非法的。可能是复制/粘贴错误。找到正确的计算机名称属性后,选择所有计算机节点并将凭据添加到每个计算机节点。相反,您不应该选择属性,而应该选择符合机器名称的元素:
未经测试但应该有效(或向您展示方向)
After indentation I saw, that you xml is not well-formatted. There is one
<cred-pairs>
opening tag right before</machine>
and that's illegal at that place. 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:
Untested but should work (or show you the direction)