package com.test.bao;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
* org.jdom 读取xml文件
* @author Administrator
*
*/
public class DocTest {
private static String path="src/com/test/bao/test.xml";
public String parseXml(String filepath,String name) throws JDOMException, IOException{
SAXBuilder builder=new SAXBuilder(false);
Document document=builder.build(filepath);
Element root=document.getRootElement();
List<Element> messList = root.getChildren("mess");
Element childrenRoot = null;
List<Element> propertyList = null;
for(int i = 0; i < messList.size(); i++){
//将根元素prop下的mess子元素作为一个新的子根元素
childrenRoot = messList.get(i);
//获取子根元素mess下的所有property子元素
propertyList = childrenRoot.getChildren("property");
//遍历子根元素的子元素集合(即遍历property元素)
for(int j = 0; j < propertyList.size(); j++){
//获取property元素
Element element = propertyList.get(j);
//element.getAttributeValue("name"):获取property中name属性的值
if(element.getAttributeValue("name").equals(name)){ //如果name的值一致
return element.getAttributeValue("value"); //取得name对应的value属性值
}
}
}
//遍历完没有查找到结果返回null
return null;
}
public static void main(String[] args) throws JDOMException, IOException{
DocTest doc=new DocTest();
String data=doc.parseXml(path, "pSize");
String data1=doc.parseXml(path, "PSize");
System.out.println("第一个数据"+data +" 第二个数据"+data1);
}
}
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
/**
* org.jdom 读取xml文件
* @author Administrator
*
*/
public class DocTest {
private static String path="src/com/test/bao/test.xml";
public String parseXml(String filepath,String name) throws JDOMException, IOException{
SAXBuilder builder=new SAXBuilder(false);
Document document=builder.build(filepath);
Element root=document.getRootElement();
List<Element> messList = root.getChildren("mess");
Element childrenRoot = null;
List<Element> propertyList = null;
for(int i = 0; i < messList.size(); i++){
//将根元素prop下的mess子元素作为一个新的子根元素
childrenRoot = messList.get(i);
//获取子根元素mess下的所有property子元素
propertyList = childrenRoot.getChildren("property");
//遍历子根元素的子元素集合(即遍历property元素)
for(int j = 0; j < propertyList.size(); j++){
//获取property元素
Element element = propertyList.get(j);
//element.getAttributeValue("name"):获取property中name属性的值
if(element.getAttributeValue("name").equals(name)){ //如果name的值一致
return element.getAttributeValue("value"); //取得name对应的value属性值
}
}
}
//遍历完没有查找到结果返回null
return null;
}
public static void main(String[] args) throws JDOMException, IOException{
DocTest doc=new DocTest();
String data=doc.parseXml(path, "pSize");
String data1=doc.parseXml(path, "PSize");
System.out.println("第一个数据"+data +" 第二个数据"+data1);
}
}