DOM和SAX解析xml文档--初学例子
student.xml需要解析的xml文件:
<?xml version="1.0" encoding="GBK"?>
<StudentInfo>
<student>
<name>张三</name>
<sex>男</sex>
<lesson>
<lessonName>Spring整合开发</lessonName>
<lessonScore>85</lessonScore>
</lesson>
<lesson>
<lessonName>轻量级J2EE应用开发</lessonName>
<lessonScore>95</lessonScore>
</lesson>
<lesson>
<lessonName>Ajax应用开发</lessonName>
<lessonScore>80</lessonScore>
</lesson>
</student>
<student>
<name>王小梅</name>
<sex>女</sex>
<lesson>
<lessonName>Word快速入门</lessonName>
<lessonScore>80</lessonScore>
</lesson>
<lesson>
<lessonName>Excel应用</lessonName>
<lessonScore>95</lessonScore>
</lesson>
<lesson>
<lessonName>使用OutLook</lessonName>
<lessonScore>80</lessonScore>
</lesson>
</student>
</StudentInfo>
Dom解析:
package lee;import org.w3c.dom.*;import java.io.*;import java.util.*;import org.apache.xerces.parsers.DOMParser;import org.junit.Test;/** * @author yeeku.H.lee kongyeeku@163.com * @version 1.0 *
This program is protected by copyright laws. * package lee;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import org.apache.xerces.parsers.DOMParser;
import org.junit.Test;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class TestDOMParser
{
public static void main(String[] args)
{
TestDOMParser tp = new TestDOMParser();
tp.parseXMLFile("\\dom\\student.xml");
}
@Test
public void test(){
TestDOMParser tp = new TestDOMParser();
tp.parseXMLFile("student.xml");
}
/**
* 解析文档
* @param fileName
*/
public void parseXMLFile(String fileName)
{
try
{
DOMParser parser = new DOMParser();
parser.parse(fileName);
//getDocument()获取Document对象
Document doc = parser.getDocument();
//获取root节点
Element elmtInfo = doc.getDocumentElement();
//getElementsByTagName()根据标签名获取节点列表 : 利用父子关系获取子节点
NodeList nlStudent = elmtInfo.getElementsByTagName("student");
System.out.println("XML文件开始解析");
System.out.println();
//循环输出每一个学生成绩
for (int i = 0; i < nlStudent.getLength(); i++)
{
//当前student元素
Element elmtStudent = (Element) nlStudent.item(i);
//利用父子关系获取子节点
NodeList nlCurrent = elmtStudent.getElementsByTagName("name");
System.out.println("姓名:" + nlCurrent.item(0).getFirstChild().getNodeValue());
nlCurrent = elmtStudent.getElementsByTagName("sex");
System.out.println("性别:" + nlCurrent.item(0).getFirstChild().getNodeValue());
nlCurrent = elmtStudent.getElementsByTagName("lesson");
for (int j = 0; j < nlCurrent.getLength(); j++)
{
Element elmtLesson = (Element) nlCurrent.item(j);
NodeList nlLesson = elmtLesson.getElementsByTagName("lessonName");
System.out.print(nlLesson.item(0).getFirstChild().getNodeValue());
System.out.print(":");
nlLesson = elmtLesson.getElementsByTagName("lessonScore");
System.out.print(nlLesson.item(0).getFirstChild().getNodeValue());
System.out.println();
}
System.out.println("------------------------------------");
}
System.out.println("XML文件解析结束");
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------------------
SAX解析:
package lee;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class SAXParser
{
public static void main(String[] args)
{
SAXParser sax = new SAXParser();
sax.parseXMLFile("student.xml");
}
// 解析文档
private void parseXMLFile(String fileName)
{
try
{
//通过指定解析器的名称来动态加载解析器
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
//处理内容前要注册内容管理器
parser.setContentHandler(new XMLContentHandler());
//开始解析文档
parser.parse(fileName);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
}
}
package lee;
import org.xml.sax.*;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class XMLContentHandler implements ContentHandler
{
//DTD中定义的元素名
private static final String ELEMENT_NAME = "name";
private static final String ELEMENT_SEX = "sex";
private static final String ELEMENT_LESSON = "lesson";
private static final String ELEMENT_LESSON_NAME = "lessonName";
private static final String ELEMENT_LESSON_SCORE = "lessonScore";
private static final String ELEMENT_STUDENT = "student";
private static final String ELEMENT_LINE = "breakLine";
//当前元素的数据
private String currentData = "";
private String lessonName = "";
private String lessonScore = "";
//当其他某一个调用事件发生时,先调用此方法来在文档中定位。
public void setDocumentLocator(Locator locator)
{
}
//在解析整个文档开始时调用
public void startDocument() throws SAXException
{
System.out.println("XML文件开始解析");
}
//在解析整个文档结束时调用
public void endDocument() throws SAXException
{
System.out.println("XML文件解析结束");
}
// 在解析命名空间开始时调用
public void startPrefixMapping(String prefix, String uri) throws SAXException
{
System.out.println("XML解析器开始解析命名空间");
}
//在解析命名空间结束时调用
public void endPrefixMapping(String prefix) throws SAXException
{
System.out.println("XML解析器解析命名空间结束");
}
//在解析元素开始时调用
public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
{
System.out.println("XML解析器开始解析元素" + localName);
}
//在解析元素结束时调用
public void endElement(String namespaceURI, String localName, String qName) throws SAXException
{
if (localName.equals(ELEMENT_NAME))
{
System.out.println(localName + " : " + currentData);
}
if (localName.equals(ELEMENT_SEX))
{
System.out.println(localName + " : " + currentData);
}
if (localName.equals(ELEMENT_LESSON_NAME))
{
this.lessonName = currentData;
}
if (localName.equals(ELEMENT_LESSON_SCORE))
{
this.lessonScore = currentData;
}
if (localName.equals(ELEMENT_LESSON))
{
System.out.println(lessonName + " : " + lessonScore);
}
System.out.println("XML解析器解析元素" + localName + "结束");
}
// 取得元素数据
public void characters(char[] ch, int start, int length) throws SAXException
{
currentData = new String(ch, start, length).trim();
System.out.println("XML解析器成功解析到元素数据");
}
//取得元素数据中的空白
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
{
}
//在解析到处理指令时,调用此方法。
public void processingInstruction(String target, String data) throws SAXException
{
}
//当未验证解析器忽略实体时调用此方法
public void skippedEntity(String name) throws SAXException
{
}
}