用SAX解析XML實(shí)例
實(shí)例:以下輸出中的所有屬性和標(biāo)簽值
package com.meixin.xml; import java.io.File;
import java.util.HashMap;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class PraseXML extends DefaultHandler
{
private Vector<String> tagName;
private Vector<String> tagValue;
private int step;
// 開(kāi)始解析XML文件
public void startDocument() throws SAXException
{
tagName = new Vector<String>();
tagValue = new Vector<String>();
step = 0;
}
// 結(jié)束解析XML文件
public void endDocument() throws SAXException
{
for (int i = 0; i < tagName.size(); i++)
{
if (!tagName.get(i).equals("") || tagName.get(i) != null)
{
System.out.println("節(jié)點(diǎn)名稱:" + tagName.get(i));
System.out.println("節(jié)點(diǎn)值:" + tagValue.get(i));
}
}
}
/**
* 在解釋到一個(gè)開(kāi)始元素時(shí)會(huì)調(diào)用此方法.但是當(dāng)元素有重復(fù)時(shí)可以自己寫(xiě)算法來(lái)區(qū)分
* 這些重復(fù)的元素.qName是什么? <name:page ll=""></name:page>這樣寫(xiě)就會(huì)拋出SAXException錯(cuò)誤
* 通常情況下qName等于localName
*/
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
// 節(jié)點(diǎn)名稱
tagName.add(qName);
// 循環(huán)輸出屬性
for (int i = 0; i < attributes.getLength(); i++)
{
// 獲取屬性名稱
System.out.println("屬性名稱:" + attributes.getQName(i));
// 獲取屬性值
System.out.println("屬性值:"
+ attributes.getValue(attributes.getQName(i)));
}
}
/**
* 在遇到結(jié)束標(biāo)簽時(shí)調(diào)用此方法
*/
public void endElement(String uri, String localName, String qName)
throws SAXException
{
step = step + 1;
}
/**
* 讀取標(biāo)簽里的值,ch用來(lái)存放某行的xml的字符數(shù)據(jù),包括標(biāo)簽,初始大小是2048,
* 每解釋到新的字符會(huì)把它添加到char[]里。 * 注意,這個(gè)char字符會(huì)自己管理存儲(chǔ)的字符,
* 并不是每一行就會(huì)刷新一次char,start,length是由xml的元素?cái)?shù)據(jù)確定的,
* 暫時(shí)找不到規(guī)律,以后看源代碼.
*
* 這里一個(gè)正標(biāo)簽,反標(biāo)簽都會(huì)被執(zhí)行一次characters,所以在反標(biāo)簽時(shí)不用獲得其中的值
*/
public void characters(char ch[], int start, int length)
throws SAXException
{
// 只要當(dāng)前的標(biāo)簽組的長(zhǎng)度一至,值就不賦,則反標(biāo)簽不被計(jì)劃在內(nèi)
if (tagName.size() - 1 == tagValue.size())
{
tagValue.add(new String(ch, start, length));
}
}
public static void main(String[] args)
{
String filename = "MyXml.xml";
SAXParserFactory spf = SAXParserFactory.newInstance();
try
{
SAXParser saxParser = spf.newSAXParser();
saxParser.parse(new File(filename), new PraseXML());
}
catch (Exception e)
{
e.printStackTrace();
}
}
public Vector getTagName()
{
return tagName;
}
public void setTagName(Vector tagName)
{
this.tagName = tagName;
}
public Vector getTagValue()
{
return tagValue;
}
public void setTagValue(Vector tagValue)
{
this.tagValue = tagValue;
}
}
輸出結(jié)果:
屬性名稱:personid
屬性值:e01
屬性名稱:enable
屬性值:true
屬性名稱:personid
屬性值:e02
屬性名稱:enable
屬性值:false
屬性名稱:personid
屬性值:e03
屬性名稱:enable
屬性值:true
節(jié)點(diǎn)名稱:people
節(jié)點(diǎn)值:
節(jié)點(diǎn)名稱:person
節(jié)點(diǎn)值:
節(jié)點(diǎn)名稱:name
節(jié)點(diǎn)值:張三
節(jié)點(diǎn)名稱:tel
節(jié)點(diǎn)值:5128
節(jié)點(diǎn)名稱:email
節(jié)點(diǎn)值:txq512@sina.com
節(jié)點(diǎn)名稱:person
節(jié)點(diǎn)值:
節(jié)點(diǎn)名稱:name
節(jié)點(diǎn)值:meixin
節(jié)點(diǎn)名稱:tel
節(jié)點(diǎn)值:5252525
節(jié)點(diǎn)名稱:email
節(jié)點(diǎn)值:wnight88@sina.com
節(jié)點(diǎn)名稱:person
節(jié)點(diǎn)值:
節(jié)點(diǎn)名稱:name
節(jié)點(diǎn)值:yu
節(jié)點(diǎn)名稱:tel
節(jié)點(diǎn)值:5389654
節(jié)點(diǎn)名稱:email
節(jié)點(diǎn)值:yu@188.net
屬性值:e01
屬性名稱:enable
屬性值:true
屬性名稱:personid
屬性值:e02
屬性名稱:enable
屬性值:false
屬性名稱:personid
屬性值:e03
屬性名稱:enable
屬性值:true
節(jié)點(diǎn)名稱:people
節(jié)點(diǎn)值:
節(jié)點(diǎn)名稱:person
節(jié)點(diǎn)值:
節(jié)點(diǎn)名稱:name
節(jié)點(diǎn)值:張三
節(jié)點(diǎn)名稱:tel
節(jié)點(diǎn)值:5128
節(jié)點(diǎn)名稱:email
節(jié)點(diǎn)值:txq512@sina.com
節(jié)點(diǎn)名稱:person
節(jié)點(diǎn)值:
節(jié)點(diǎn)名稱:name
節(jié)點(diǎn)值:meixin
節(jié)點(diǎn)名稱:tel
節(jié)點(diǎn)值:5252525
節(jié)點(diǎn)名稱:email
節(jié)點(diǎn)值:wnight88@sina.com
節(jié)點(diǎn)名稱:person
節(jié)點(diǎn)值:
節(jié)點(diǎn)名稱:name
節(jié)點(diǎn)值:yu
節(jié)點(diǎn)名稱:tel
節(jié)點(diǎn)值:5389654
節(jié)點(diǎn)名稱:email
節(jié)點(diǎn)值:yu@188.net
文件內(nèi)容
<?xml version="1.0" encoding="UTF-8"?>
<people>
<person personid="e01" enable="true">
<name>張三</name>
<tel>5128</tel>
<email>txq512@sina.com</email>
</person>
<person personid="e02" enable="false">
<name>meixin</name>
<tel>5252525</tel>
<email>wnight88@sina.com</email>
</person>
<person personid="e03" enable="true">
<name>yu</name>
<tel>5389654</tel>
<email>yu@188.net</email>
</person>
</people>
<people>
<person personid="e01" enable="true">
<name>張三</name>
<tel>5128</tel>
<email>txq512@sina.com</email>
</person>
<person personid="e02" enable="false">
<name>meixin</name>
<tel>5252525</tel>
<email>wnight88@sina.com</email>
</person>
<person personid="e03" enable="true">
<name>yu</name>
<tel>5389654</tel>
<email>yu@188.net</email>
</person>
</people>