下面是xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person perid="1001">
<name>lhu</name>
<age>89</age>
<address>安徽淮北</address>
<sex>男</sex>
</person>
<person perid="1002">
<name>we</name>
<age>56</age>
<address>北京海淀</address>
<sex>女</sex>
</person>
</persons>
下面是解析上面的xml文件:
通过JDOM来解析,需要借助第三方的组件.jdom.jar,网上有1.0的版本下载
package cn.com.jdom;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.ProcessingInstruction;
import org.jdom.input.SAXBuilder;
import com.sun.xml.internal.bind.v2.runtime.Name;
/**
* jdom解析xml文件
*
* @author ly *
*/
public class JDomXML {
public JDomXML() {
}
/**
* 解析xml文件
* @param xmlFile
*/
public void parseXml(File xmlFile) {
SAXBuilder sax = new SAXBuilder();//在内存中建立一个sax文档模型
try {
Document xmlDom = sax.build(xmlFile);//创建文档
//获得文件的根元素
Element root = xmlDom.getRootElement();
System.out.println("根元素是:"+root.getName());
//获得根元素的子节点
List childList = root.getChildren();
Iterator listIt = childList.iterator();
while(listIt.hasNext()){
Element element = (Element)listIt.next();
System.out.println("孩子结点是:"+element.getName());
}
//获得第一个孩子结点
Element firstChild = (Element) childList.get(0);
//获得孩子结点的属性
List attrList = firstChild.getAttributes();
Iterator attrIt = attrList.iterator();
while(attrIt.hasNext()){
Attribute attr = (Attribute ) attrIt.next();
System.out.println("第一个元素的属性是:"+attr.getName());
//获得属性的值
System.out.println("属性的值是:"+attr.getValue());
//获得属性的类型
System.out.println("属性的类型是:"+attr.getAttributeType());
}
List sonList = firstChild.getChildren();
Iterator sonIt = sonList.iterator();
while(sonIt.hasNext()){
Element temp = (Element)sonIt.next();
System.out.println("属性"+temp.getName()+"的值是:"+temp.getValue());
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JDomXML test = new JDomXML();
test.parseXml(new File("persons.xml"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person perid="1001">
<name>lhu</name>
<age>89</age>
<address>安徽淮北</address>
<sex>男</sex>
</person>
<person perid="1002">
<name>we</name>
<age>56</age>
<address>北京海淀</address>
<sex>女</sex>
</person>
</persons>
下面是解析上面的xml文件:
通过JDOM来解析,需要借助第三方的组件.jdom.jar,网上有1.0的版本下载
package cn.com.jdom;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.ProcessingInstruction;
import org.jdom.input.SAXBuilder;
import com.sun.xml.internal.bind.v2.runtime.Name;
/**
* jdom解析xml文件
*
* @author ly *
*/
public class JDomXML {
public JDomXML() {
}
/**
* 解析xml文件
* @param xmlFile
*/
public void parseXml(File xmlFile) {
SAXBuilder sax = new SAXBuilder();//在内存中建立一个sax文档模型
try {
Document xmlDom = sax.build(xmlFile);//创建文档
//获得文件的根元素
Element root = xmlDom.getRootElement();
System.out.println("根元素是:"+root.getName());
//获得根元素的子节点
List childList = root.getChildren();
Iterator listIt = childList.iterator();
while(listIt.hasNext()){
Element element = (Element)listIt.next();
System.out.println("孩子结点是:"+element.getName());
}
//获得第一个孩子结点
Element firstChild = (Element) childList.get(0);
//获得孩子结点的属性
List attrList = firstChild.getAttributes();
Iterator attrIt = attrList.iterator();
while(attrIt.hasNext()){
Attribute attr = (Attribute ) attrIt.next();
System.out.println("第一个元素的属性是:"+attr.getName());
//获得属性的值
System.out.println("属性的值是:"+attr.getValue());
//获得属性的类型
System.out.println("属性的类型是:"+attr.getAttributeType());
}
List sonList = firstChild.getChildren();
Iterator sonIt = sonList.iterator();
while(sonIt.hasNext()){
Element temp = (Element)sonIt.next();
System.out.println("属性"+temp.getName()+"的值是:"+temp.getValue());
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JDomXML test = new JDomXML();
test.parseXml(new File("persons.xml"));
}
}