一、Sax解析格式实例
test.xml
注意本段解析文件中<heading> 和<body>之中都有<font>,所以要考虑一一对应问题
以<to>George</to>为例:startElement读取到了qName为to标签,紧接着character函数运行获取标签后的内容George,endElement函数读取/to标签,以此类推执行下去。
<note level="5">
<to>George</to>
<to>Jack</to>
<to>Jash</to>
<to>Ryan</to>
<from>John</from>
<heading>
<font>正黑</font>
<title>Reminder</title>
</heading>
<body>
<font>宋体</font>
<message>Don't forget the meeting!</message>
</body>
</note>
TestSax.java
public class Test {
public static void main(String[] args) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
Note note = new Note();
parser.parse(new File("res/test.xml"), new MyHandler(note));
System.out.println(note.getHeading());
System.out.println(note.getBody());
System.out.println(note.getLevel());
System.out.println(note.getTo());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Note.java
public class Note {
private List<String> to;
private String from;
private Heading heading;
private Body body;
private int level;
public List<String> getTo() {
if (to == null) {
to = new ArrayList<>();
}
return to;
}
public void setTo(List<String> to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public Heading getHeading() {
return heading;
}
public void setHeading(Heading heading) {
this.heading = heading;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
Heading.java
public class Heading {
private String font;
private String title;
public String getFont() {
return font;
}
public void setFont(String font) {
this.font = font;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Heading{" +
"font='" + font + '\'' +
", title='" + title + '\'' +
'}';
}
}
Body.java
public class Body {
private String font;
private String message;
public String getFont() {
return font;
}
public void setFont(String font) {
this.font = font;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Body{" +
"font='" + font + '\'' +
", message='" + message + '\'' +
'}';
}
}
MyHandler.java
public class MyHandler extends DefaultHandler {
private Note note;
private Stack<String> nameStack;
public MyHandler(Note note) {
this.note = note;
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
nameStack = new Stack<>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
nameStack.push(qName);//qName表示的是note,form,heading,font这样的标签。
switch (qName) {
case "note":
note.setLevel(Integer.parseInt(attributes.getValue("level")));
break;
case "heading":
note.setHeading(new Heading());
break;
case "body":
note.setBody(new Body());
break;
}
// System.out.println(qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
String s = new String(ch, start, length).trim();
System.out.println(ch);
System.out.println("=======");
if (!s.isEmpty()) {
// System.out.println(s);
switch (nameStack.peek()) {
case "to":
note.getTo().add(s);
break;
case "from":
note.setFrom(s);
break;
case "font":
switch (nameStack.get(nameStack.size() - 2)) {//下标从零开始的所以要减2
case "heading":
note.getHeading().setFont(s);
break;
case "body":
note.getBody().setFont(s);
break;
}
break;
case "title":
note.getHeading().setTitle(s);
break;
case "message":
note.getBody().setMessage(s);
break;
// case "heading":
// note.setHeading(s);
// break;
// case "body":
// note.setBody(s);
// break;
}
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
nameStack.pop();//用来是释放栈的内存
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
}