解析下面的xml文档
<?xml version="1.0" encoding="UTF-8"?>
<exam>
<student examcard="1" idcard="001">
<name>李华</name>
<location>上海</location>
<grade>90</grade>
</student>
<student examcard="2" idcard="002">
<name>张三</name>
<location>北京</location>
<grade>80</grade>
</student>
<student examcard="3" idcard="003">
<name>王</name>
<location>武汉</location>
<grade>90</grade>
</student>
</exam>
用下面的程序解析
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class Dome1 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//创建sax的解析工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//得到解析器
SAXParser sax=factory.newSAXParser();
//得到读取器
XMLReader read=sax.getXMLReader();
//设置事件处理器
read.setContentHandler(new handler());
//读取本地资源
//read.parse("G:\\JAVA TEST\\SaxTest\\src\\student.xml");
read.parse("src/student.xml");
}
}
class handler implements ContentHandler{
public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
System.out.println("<"+"name"+">");
}
public void characters(char[] ch, int start, int length) throws SAXException {
System.out.println(new String(ch,start,length));
}
public void endElement(String arg0, String arg1, String arg2) throws SAXException {
System.out.println("</"+"name"+">");
}
public void endDocument() throws SAXException {
}
public void endPrefixMapping(String arg0) throws SAXException {
}
public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
}
public void processingInstruction(String arg0, String arg1) throws SAXException {
}
@Override
public void setDocumentLocator(Locator arg0) {
// TODO 自动生成的方法存根
}
@Override
public void skippedEntity(String arg0) throws SAXException {
// TODO 自动生成的方法存根
}
@Override
public void startDocument() throws SAXException {
// TODO 自动生成的方法存根
}
@Override
public void startPrefixMapping(String arg0, String arg1) throws SAXException {
// TODO 自动生成的方法存根
}
}
选择性解析
import java.io.IOException;
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.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class Dome {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser sax=factory.newSAXParser();
XMLReader read=sax.getXMLReader();
//设置事件处理器
read.setContentHandler(new ContentHandler());
read.parse("src/student.xml");
}
}
class ContentHandler extends DefaultHandler{
//记住当前解析到的是什么标签
private String contenttag;
private int neednumber=2; //设置要获取的标签索引,这里要获取第二个
private int currentnumber; //设置解析器解析当前标签的索引
public void startElement(String arg0, String arg1, String name, Attributes arg3) throws SAXException {
contenttag=name;
if("name".equals(contenttag)){
currentnumber++; //记录解析标签的索引
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
//若解析当前标签 等于 要获取的标签,则获取该name
if("name".equals(contenttag)&¤tnumber==neednumber){
System.out.println(new String(ch,start,length));
}
}
public void endElement(String arg0, String arg1, String arg2) throws SAXException {
contenttag=null;
}
}