java jdom xml 解析_java解析xml(使用jdom解析xml)

package ReadJdom;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.jdom2.Document;

import org.jdom2.Element;

import org.jdom2.JDOMException;

import org.jdom2.input.SAXBuilder;

public class ReadJm {

public static void main(String[] args) throws JDOMException, IOException {

//新建构造器解析xml

SAXBuilder sax = new SAXBuilder();

//建一个文档去接受数据

Document doc = sax.build("readjm.xml");

//获取根元素

Element root = doc.getRootElement();

//定位到 -> Listlist = root.getChildren("Key");

List children = new ArrayList();

List childrens = new ArrayList();

System.out.println( list.size());

for (int i = 0; i

Element element=(Element)list.get(i);

System.out.println(element.getAttributeValue("Name"));//第二层节点

children= element.getChildren("Value");for (int j= 0;j < children.size(); j++) {

//System.out.println(children.size());子节点下面有多少个位Value的KV

Element elementC=(Element)children.get(j);//第三层节点

//System.out.println(elementC.getAttributeValue("Name"));得到第三层节点属性为Name的值

//定位到 -> -> if(elementC.getAttributeValue("Name").equals("PhotoIDWidth")){

//获取 -> -> 属性值

System.out.print(""+elementC.getAttributeValue("Name"));

//获取 -> -> 标签里内容

System.out.print(","+elementC.getText());

}

}

children.clear();

//定位到 -> -> children = element.getChildren("Key");

for (int k = 0; k

Element elementChildren=(Element)children.get(k);

//定位到 -> -> if(elementChildren.getAttributeValue("Name").equals("Child")){

childrens = elementChildren.getChildren("Value");

for (int j = 0; j

Element elementChildrens=(Element)childrens.get(j);//注意这里是childrens 0.10这里是第四层

//定位到 -> -> -> if(elementChildrens.getAttributeValue("Name").equals("HeadPercent")){

System.out.println(""+elementChildrens.getText());

}

}

}

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值