java四大解析xml文件的方式

myusers.xml

<?xml version="1.0" encoding="GBK"?>
<users xmlns="http://www.softeem.com/xml">
<user id="1">
<name>mick</name>
<pass>123</pass>
</user>
<user id="2">
<name>jack</name>
<pass>123456</pass>
</user>
</users>

第一种:DOM解析方式

package com.softeem.xml.demo;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestDOM {

public static void main(String[] args) {
DocumentBuilderFactory factory =
   DocumentBuilderFactory.newInstance();
try {
   DocumentBuilder builder =
    factory.newDocumentBuilder();
   Document doc = builder.parse(new File
   ("src/com/softeem/xml/demo/myusers.xml"));
   NodeList nodeList = doc.getElementsByTagName("user");
   for (int i = 0; i < nodeList.getLength(); i++) {
    Element user = (Element)nodeList.item(i);
    System.out.println("id:"+user.getAttribute("id"));
    String name = user.getElementsByTagName("name").item(i)
    .getFirstChild().getNodeValue();   
    System.out.println("name:"+name);
    String pass = user.getElementsByTagName("pass").item(i)
    .getFirstChild().getNodeValue();
    System.out.println("pass:"+pass);
   }
} catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
}
}

第二种:SAX解析方式

package com.softeem.xml.demo;

import java.io.File;
import java.io.IOException;
import java.util.Stack;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TestSAX extends DefaultHandler{

//定义一个栈   先进后出
Stack stack = new Stack();

public static void main(String[] args) {
SAXParserFactory factory =
   SAXParserFactory.newInstance();
try {
   SAXParser parser = factory.newSAXParser();
   parser.parse(new File("src/com/softeem" +
"/xml/demo/myusers.xml"), new TestSAX());
} catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
}

public void startDocument(){
System.out.println("start......");
}

//接受文档元素开始的通知
//uri名称空间的URI
//localName 本地名称
//name 限定名称节点的值
//attributes 附加到元素的属性
public void startElement(String uri,String localName,
   String name,Attributes attributes){
//把项压入栈的顶部
stack.push(name);
String id = attributes.getValue("id");
if(id != null){
   System.out.println("id:"+id);
}
}
//接受元素字符数据的通知
//ch 整个文档的字符
//start 字符数据中的开始位置
//length 从字符数据中使用的字符长度
public void characters(char[] ch,int start,int length){
String value = new String(ch,start,length);
//查看栈顶
String name = (String)stack.peek();
if(name.equals("name")){
   System.out.println("name:"+value);
}else if(name.equals("pass")){
   System.out.println("pass:"+value);
}
}

//接受元素结束的通知
public void endElement(String uri,String localName
   ,String name){
//移除栈顶的对象
stack.pop();
}

//接受文档结束的通知
public void endDocument(){
try {
   super.endDocument();
} catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
}

}

第三种:JDOM解析方式

package com.softeem.xml.demo;

import java.io.File;
import java.io.IOException;
import java.util.List;

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

public class TestJDOM {

public static void main(String[] args) {
SAXBuilder builder = new SAXBuilder();
try {
   Document doc = builder.
   build(new File("src/com/softeem/xml/demo/myusers.xml"));
   Element root = doc.getRootElement();
   Namespace ns = root.getNamespace();
   System.out.println("ns:"+ns);
   List list = root.getChildren();
   for (int i = 0; i < list.size(); i++) {
    Element user = (Element)list.get(i);
    String id = user.getAttributeValue("id");   
    System.out.println("id:"+id);
    String name = user.getChild("name",ns).getText();
    System.out.println("name:"+name);
    String pass = user.getChild("pass",ns).getText();
    System.out.println("pass:"+pass);
   }  
} catch (JDOMException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
}

}

第四种:DOM4J解析方式

package com.softeem.xml.demo;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class TestDOM4J {

public static void main(String[] args) {
SAXReader reader = new SAXReader();
try {
   Document doc = reader.read(
new File("src/com/softeem/xml/demo/myusers.xml"));
   Element root = doc.getRootElement();
   Iterator it = root.elementIterator();
   while(it.hasNext()){
    Element user = (Element)it.next();
    String id = user.attributeValue("id");
    System.out.println("id:"+id);
    String name = user.elementText("name");
    System.out.println("name:"+name);
    String pass = user.elementText("pass");
    System.out.println("pass:"+pass);
   }
  
} catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
}
}

http://hi.baidu.com/zengfeng75/blog/item/1a8f14f7ec85ca33730eecf1.html

你可能感兴趣的:(java四大解析xml文件的方式)