java 写入xml如何在一节点下添加多个同名节点呀

v597115875 2011-04-18 09:36:19
这是xml的结构,我想在detailed节点下添加下面多个同名attr的节点,并在每个attr节点下添加它的attrlabl和attrdef子结点.
- <detailed>
- <attr>
<attrlabl>ST_CD</attrlabl>
<attrdef>Street Code</attrdef>
</attr>
</detailed>

如果把attr一个节点删除,我现在可以成功添加一次attr节点用下面的代码:
Dim oRoot As IXMLDOMElement
Set oRoot = xml_document.selectSingleNode("detailed")
Dim oattr As IXMLDOMElement
Set oattr = xml_document.CreateElement("attr")
oRoot.appendChild oattr
Set oRoot = xml_document.selectSingleNode("detailed/attr")
Set oAttrlabl = xml_document.CreateElement("attrlabl")
Set oAttrdef = xml_document.CreateElement("attrdef")
oAttrlabl.Text = "ww"
oAttrdef.Text = "qq"
oRoot.appendChild oAttrlabl
oRoot.appendChild oAttrdef
执行完之后就覆盖了前面的那个节点,请问要怎么再重复添加同名的attr节点呢?

...全文
1055 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
小绵羊 2011-04-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tandy_000 的回复:]

public void toWrite(List<Deploy> deploies) {
Element root = document.createElement("data");
document.appendChild(root);

for (Deploy deploy : deploies) {
Element personRoot = document.createElem……
[/Quote]

+1
wangju309 2011-04-18
  • 打赏
  • 举报
回复
多次appendChild
Set oRoot = xml_document.selectSingleNode("detailed/attr")这用single不合适吧?
司马喂羊 2011-04-18
  • 打赏
  • 举报
回复
public void toWrite(List<Deploy> deploies) {
Element root = document.createElement("data");
document.appendChild(root);

for (Deploy deploy : deploies) {
Element personRoot = document.createElement("person");
personRoot.setAttribute("id", deploy.getId());
root.appendChild(personRoot);

Element email = document.createElement("email");
email.appendChild(document.createTextNode(deploy.getEmail()));
personRoot.appendChild(email);

Element password = document.createElement("password");
password.appendChild(document.createTextNode(deploy.getPassword()));
personRoot.appendChild(password);

Element pop3 = document.createElement("pop3");
pop3.appendChild(document.createTextNode(deploy.getPop3_service()));
personRoot.appendChild(pop3);

Element smtp = document.createElement("smtp");
smtp.appendChild(document.createTextNode(deploy.getSmtp_service()));
personRoot.appendChild(smtp);
}
}


这个方法生成的xml,根据传入的List<Deploy> deploies参数来配置多个person对象信息
结果是这样的:
<data>
<person id="1">
<email></email>
<password></password>
<pop3></pop3>
<smtp></smtp>
</person>
<person id="2">
<email></email>
<password></password>
<pop3></pop3>
<smtp></smtp>
</person>
<person id="3">
<email></email>
<password></password>
<pop3></pop3>
<smtp></smtp>
</person>
</data>

希望对你有用!
wkchina 2011-04-18
  • 打赏
  • 举报
回复
同不同名字不影响的,关键是层次对了就可以,可以添加节点,节点属性。
dom4j操作xml很好用的,可以试试!
v597115875 2011-04-18
  • 打赏
  • 举报
回复
具体是怎么实现的啊!
司马喂羊 2011-04-18
  • 打赏
  • 举报
回复
解析原来的xml文件
v597115875 2011-04-18
  • 打赏
  • 举报
回复
其实我的问题是怎么样在原有的xml中加入相同的节点

假如说 我本来有了一个这个xml文件里面本身有内容
<detailed>
<attr>
<attrlabl>ST_CD</attrlabl>
<attrdef>Street Code</attrdef>
</attr>
</detailed>
但是我要在往里面加入相同的节点内容,就变成这样
<detailed>
<attr>
<attrlabl>ST_CD</attrlabl>
<attrdef>Street Code</attrdef>
</attr>

<attr>
<attrlabl>aaaaaa</attrlabl>
<attrdef>bbbbbbb</attrdef>
</attr>
</detailed>
我每次创建了之后就把原来的那个节点都覆盖了,怎么样达到这种效果阿

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧