📄 sax1.java
字号:
package xmloperation;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
import java.io.*;
public class sax1 extends DefaultHandler
{
Stack tags = new Stack();
String name;
String address;
String tel;
String fax;
String email;
public sax1() {}
public void endDocument() throws SAXException
{
System.out.println("-----Parse End");
}
public void startDocument() throws SAXException
{
System.out.println("-----Parse Start");
}
public void startElement(String p0,String p1,String p2,Attributes p3) throws SAXException
{
tags.push(p2);
}
public void endElement(String p0,String p1,String p2) throws SAXException
{
tags.pop();
if(p2.equals("PERSON"))
{
printout();
}
}
public void characters(char[] p0,int p1,int p2) throws SAXException
{
String tag = tags.peek().toString();
if(tag.equals("NAME"))
{
name = new String(p0,p1,p2);
}
else if(tag.equals("ADDRESS"))
{
address = new String(p0,p1,p2);
}
else if(tag.equals("TEL"))
{
tel = new String(p0,p1,p2);
}
else if(tag.equals("FAX"))
{
fax = new String(p0,p1,p2);
}
else if(tag.equals("EMAIL"))
{
email = new String(p0,p1,p2);
}
}
public void printout()
{
System.out.print("NAME:");
System.out.println(name);
System.out.print("ADDRESS:");
System.out.println(address);
System.out.print("TEL:");
System.out.println(tel);
System.out.print("FAX:");
System.out.println(fax);
System.out.print("EMAIL:");
System.out.println(email);
System.out.println();
}
public static void main(String[] args)
{
String filename = "candidate.xml";
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = null;
try
{
saxParser = spf.newSAXParser();
saxParser.parse(new File(filename),new sax1());
}
catch(Exception e)
{
System.out.println(e);
System.exit(1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -