Expand|Select|Wrap|Line Numbers
- import java.io.*;
- import org.xml.sax.*;
- import org.xml.sax.helpers.DefaultHandler;
- import javax.xml.parsers.SAXParserFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- public class java_xml extends DefaultHandler{
- public static void main(String argv[])
- {
- // Use an instance of ourselves as the SAX event handler
- DefaultHandler handler = new java_xml();
- // Use the default (non-validating) parser
- SAXParserFactory factory = SAXParserFactory.newInstance();
- try {
- // Set up output stream
- OutputStreamWriter out = new OutputStreamWriter(System.out, "UTF8");
- // Parse the input
- SAXParser saxParser = factory.newSAXParser();
- saxParser.parse( "file.xml", handler );
- } catch (SAXException t) {
- System.err.println(t.getLocalizedMessage()+" Errorzz");
- }
- catch (IOException t) {
- System.err.println(t.getLocalizedMessage()+" Errorzz");
- }
- catch (ParserConfigurationException t) {
- t.printStackTrace();
- }
- System.exit(0);
- }
- static private Writer out;
- private void emit(String s)
- throws SAXException
- {
- try {
- out.write(s);
- out.flush();
- } catch (IOException e) {
- throw new SAXException("I/O error", e);
- }
- }
- private void nl()
- throws SAXException
- {
- String lineEnd = System.getProperty("line.separator");
- try {
- //ECHOING AN XML FILE WITH THE SAX PARSER 129
- out.write(lineEnd);
- } catch (IOException e) {
- throw new SAXException("I/O error", e);
- }
- }
- public void startDocument()
- throws SAXException
- {
- //emit("<?xml version='1.0' encoding='UTF-8'?>");
- //nl();
- System.out.println("parsing starts");
- }
- public void endDocument()
- throws SAXException
- {
- System.out.println("parsing Ends");
- }
- public void startElement(String namespaceURI,
- String sName, // simple name
- String qName, // qualified name
- Attributes attrs)
- throws SAXException
- {
- String eName = sName; // element name
- if ("".equals(eName)) eName = qName; // not namespace-aware
- emit("<"+eName);
- if (attrs != null) {
- for (int i = 0; i < attrs.getLength(); i++) {
- String aName = attrs.getLocalName(i); // Attr name
- if ("".equals(aName)) aName = attrs.getQName(i);
- emit(" ");
- emit(aName+"=\""+attrs.getValue(i)+"\"");
- }
- }
- emit(">");
- }
- public void endElement(String namespaceURI,
- String sName, // simple name
- String qName // qualified name
- )
- throws SAXException
- {
- //ECHOING AN XML FILE WITH THE SAX PARSER 131
- String eName = sName; // element name
- if ("".equals(eName)) eName = qName; // not namespace-aware
- emit("</"+eName+">");
- }
- }
and my xml file which I name file.xml goes below:
Expand|Select|Wrap|Line Numbers
- <?xml version='1.0' encoding='UTF-8'?>
- <Personnel>
- <Employee type="permanent">
- <Name>Seagull</Name>
- <Id>3674</Id>
- <Age>34</Age>
- </Employee>
- <Employee type="contract">
- <Name>Robin</Name>
- <Id>3675</Id>
- <Age>25</Age>
- </Employee>
- <Employee type="permanent">
- <Name>Crow</Name>
- <Id>3676</Id>
- <Age>28</Age>
- </Employee>
- </Personnel>
Why am I getting a NullPointerException?The problem is somewhat in my xml file...
Thanks in adv