jdom解析xml

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>

猜你喜欢

转载自diaochenlong2.iteye.com/blog/1830390