xml节点

Tip:节点对象

v1.获取文档的根节点.
      Element root = document.getRootElement();
v2.取得某个节点的子节点.
Element element=node.element(“书名");
v3.取得节点的文字
      String text=node.getText();
v4.取得某节点下所有名为 member 的子节点,并进行遍历.
 List nodes = rootElm.elements("member");
 
  for (Iterator it = nodes.iterator(); it.hasNext();) {
     Element elm = (Element) it.next();
    // do something
 }
v5.对某节点下的所有子节点进行遍历.
    for(Iterator it=root.elementIterator();it.hasNext();){
       Element element = (Element) it.next();
       // do something
    }
v6.在某节点下添加子节点.
Element ageElm = newMemberElm.addElement("age");
v7.设置节点文字.
 element.setText("29");
v8.删除某节点.
//childElm是待删除的节点,parentElm是其父节点
    parentElm.remove(childElm);
v9.添加一个CDATA节点.
Element contentElm = infoElm.addElement("content");
contentElm.addCDATA(diary.getContent());

 

节点对象属性 

v1.取得某节点下的某属性
     Element root=document.getRootElement();     
     //属性名name
         Attribute attribute=root.attribute("size");
v2.取得属性的文字
    String text=attribute.getText();
v 3.删除某属性
 Attribute attribute=root.attribute("size");
 root.remove(attribute);
v3.遍历某节点的所有属性
    Element root=document.getRootElement();     
   for(Iterator it=root.attributeIterator();it.hasNext();){
         Attribute attribute = (Attribute) it.next();
         String text=attribute.getText();
         System.out.println(text);
    }
v4.设置某节点的属性和文字.
   newMemberElm.addAttribute("name", "sitinspring");
v5.设置属性的文字
   Attribute attribute=root.attribute("name");
   attribute.setText("sitinspring");

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值