HelloDefaultHandlerMain
■コード
package hello.org.xml.sax;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class HelloDefaultHandlerMain extends DefaultHandler {
public void startDocument() throws SAXException {
System.out.println("start document : ");
}
public void endDocument() throws SAXException {
System.out.println("end document : ");
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("start element : " + qName);
int len = attributes.getLength();
for (int i = 0; i < len; i++) {
String value = attributes.getValue(i);
System.out.println("value:" + value);
}
}
StringBuffer sb = new StringBuffer();
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.err.println(sb.toString());
sb = new StringBuffer();
System.out.println("end element : " + qName);
}
public void characters(char ch[], int start, int length)
throws SAXException {
System.out.println("start characters : "
+ new String(ch, start, length));
sb.append(new String(ch, start, length));
}
public static void main(String[] args) throws Exception {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
HelloDefaultHandlerMain handler = new HelloDefaultHandlerMain();
saxParser.parse(new FileInputStream("files/test.xml"), handler);
}
}
■XML例
<inventory>
<book year="2000">
<title>Snow Crash</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553380958</isbn>
<price>14.95</price>
</book>
<book year="2005">
<title>Burning Tower</title>
<author>Larry Niven</author>
<author>Jerry Pournelle</author>
<publisher>Pocket</publisher>
<isbn>0743416910</isbn>
<price>5.99</price>
</book>
<book year="1995">
<title>Zodiac</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553573862</isbn>
<price>7.50</price>
</book>
<!-- more books... -->
</inventory>
■実行例
start document :
start element : inventory
start characters :
start element : book
value:2000
start characters :
start element : title
start characters : Snow Crash
end element : title
start characters :
start element : author
start characters : Neal Stephenson
end element : author
start characters :
start element : publisher
start characters : Spectra
end element : publisher
start characters :
start element : isbn
start characters : 0553380958
end element : isbn
start characters :
start element : price
start characters : 14.95
end element : price
start characters :
end element : book
start characters :
start element : book
value:2005
start characters :
start element : title
start characters : Burning Tower
end element : title
start characters :
start element : author
start characters : Larry Niven
end element : author
start characters :
start element : author
start characters : Jerry Pournelle
end element : author
start characters :
start element : publisher
start characters : Pocket
end element : publisher
start characters :
start element : isbn
start characters : 0743416910
end element : isbn
start characters :
start element : price
start characters : 5.99
end element : price
start characters :
end element : book
start characters :
start element : book
value:1995
start characters :
start element : title
start characters : Zodiac
end element : title
start characters :
start element : author
start characters : Neal Stephenson
end element : author
start characters :
start element : publisher
start characters : Spectra
end element : publisher
start characters :
start element : isbn
start characters : 0553573862
end element : isbn
start characters :
start element : price
start characters : 7.50
end element : price
start characters :
end element : book
start characters :
start characters :
end element : inventory
end document :
tags
---
Java XML SAX