欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jdom解析xml

程序员文章站 2022-05-28 10:46:17
...

package com.bjsxt;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class XmlParse {

/**
* @param args
* @throws IOException
* @throws JDOMException
*/
public static void main(String[] args) throws JDOMException, IOException {
// TODO Auto-generated method stub
SAXBuilder builder = new SAXBuilder();
InputStream file = new FileInputStream("src/xml/po.xml");
Document document = builder.build(file);//获得文档对象
Element root = document.getRootElement();//获得根节点
List<Element> list = root.getChildren();
for(Element e:list) {
System.out.println("ID="+e.getAttributeValue("id"));
System.out.println("username="+e.getChildText("username"));
System.out.println("password="+e.getChildText("password"));
}
}

}




<?xml version="1.0" encoding="UTF-8"?>
<root>
<person id="1">
<username>张三</username>
<password>123123</password>
</person>
<person id="2">
<username>1111111112</username>
<password>password2</password>
</person>

</root>
相关标签: jdom