开发者

SAXParseException returns null for getSystemId()

开发者 https://www.devze.com 2023-02-26 06:29 出处:网络
Why SAXParseException returns null for getSystemId()? What is System Identifier? import java.io.StringReader;

Why SAXParseException returns null for getSystemId()? What is System Identifier?

    import java.io.StringReader;

    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.ErrorHandler;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.XMLReader;

    public class MainClass {
      static public void main(String[] arg) throws Exception{
        boolean validate = false;

        SAXParserFactory spf = SAXParserFactory.newInstance()开发者_JAVA技巧;
        spf.setValidating(validate);

        XMLReader reader = null;
          SAXParser parser = spf.newSAXParser();
          reader = parser.getXMLReader();

        reader.setErrorHandler(new MyErrorHandler());
          reader.parse(new InputSource(new StringReader(xmlString)));
    }
      static String xmlString = "<PHONEBOOK>" +
      "  <PERSON>" +
      "   <NAME>Joe Wang</NAME>" +
      "   <EMAIL>joe@yourserver.com</EMAIL>" +
      "   <TELEPHONE>202-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>   " +
      "<NAME>Karol</NAE>" +    // error here
      "   <EMAIL>karol@yourserver.com</EMAIL>" +
      "   <TELEPHONE>306-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>" +
      "   <NAME>Green</NAME>" +
      "   <EMAIL>green@yourserver.com</EMAIL>" +
      "   <TELEPHONE>202-414-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  </PHONEBOOK>";
    }
    class MyErrorHandler implements ErrorHandler {
      public void warning(SAXParseException e) throws SAXException {
        show("Warning", e);
        throw (e);
      }

      public void error(SAXParseException e) throws SAXException {
        show("Error", e);
        throw (e);
      }

      public void fatalError(SAXParseException e) throws SAXException {
        show("Fatal Error", e);
        throw (e);
      }

      private void show(String type, SAXParseException e) {
        System.out.println(type + ": " + e.getMessage());
        System.out.println("Line " + e.getLineNumber() + " Column "
            + e.getColumnNumber());
        System.out.println("System ID: " + e.getSystemId());
        System.out.println(e);

      }
    }


The 'system identifier' in XML is the physical location you got something from. When you just parse a string in memory, it has no system identifier at all unless you make an extra call to give it one.

You can, in this case, call InputSource.setSystemId.


The System Identifier is a URI you can specify, it's there so it can be used by the EntityResolver to decide how relative paths get resolved during xml parsing. Whether it is a physical location or just a label is up to you. Of course, in your example you don't have anything to resolve so it's not needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消