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 > Spring整合开发 </ lessonName >
< lessonScore > 80 </ lessonScore >
</ lesson >
< lesson >
< lessonName > 轻量级J2EE应用开发 </ lessonName >
< lessonScore > 85 </ lessonScore >
</ lesson >
< lesson >
< lessonName > Ajax应用开发 </ lessonName >
< lessonScore > 90 </ lessonScore >
</ lesson >
</ student >
</ StudentInfo >
< 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 > Spring整合开发 </ lessonName >
< lessonScore > 80 </ lessonScore >
</ lesson >
< lesson >
< lessonName > 轻量级J2EE应用开发 </ lessonName >
< lessonScore > 85 </ lessonScore >
</ lesson >
< lesson >
< lessonName > Ajax应用开发 </ lessonName >
< lessonScore > 90 </ lessonScore >
</ lesson >
</ student >
</ StudentInfo >
步骤2.用于处理XML文档的事件监听器:
Java代码:
import
org.xml.sax.
*
;
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
{
}
}
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
{
}
}
步骤3.通过主程序开始解析XML文档,解析XML文档的代码如下:
java代码:
import
java.io.
*
;
import org.xml.sax. * ;
import org.xml.sax.helpers. * ;
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();
}
}
}
import org.xml.sax. * ;
import org.xml.sax.helpers. * ;
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();
}
}
}