SAXParseException 为 getSystemId() 返回 null

发布于 2024-11-02 10:04:52 字数 2927 浏览 4 评论 0原文

为什么 SAXParseException 对 getSystemId() 返回 null?什么是系统标识符?

    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();
        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>[email protected]</EMAIL>" +
      "   <TELEPHONE>202-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>   " +
      "<NAME>Karol</NAE>" +    // error here
      "   <EMAIL>[email protected]</EMAIL>" +
      "   <TELEPHONE>306-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>" +
      "   <NAME>Green</NAME>" +
      "   <EMAIL>[email protected]</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);

      }
    }

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();
        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>[email protected]</EMAIL>" +
      "   <TELEPHONE>202-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>   " +
      "<NAME>Karol</NAE>" +    // error here
      "   <EMAIL>[email protected]</EMAIL>" +
      "   <TELEPHONE>306-999-9999</TELEPHONE>" +
      "   <WEB>www.java2s.com</WEB>" +
      "  </PERSON>" +
      "  <PERSON>" +
      "   <NAME>Green</NAME>" +
      "   <EMAIL>[email protected]</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);

      }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-11-09 10:04:52

XML 中的“系统标识符”是您从中获取某些内容的物理位置。当您只是解析内存中的字符串时,它根本没有系统标识符,除非您进行额外的调用来给它一个系统标识符。

在这种情况下,您可以调用InputSource.setSystemId

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.

花开浅夏 2024-11-09 10:04:52

系统标识符是您可以指定的 URI,它在那里,因此 EntityResolver 可以使用它来决定在 xml 解析期间如何解析相对路径。无论是物理位置还是只是标签都取决于您。当然,在您的示例中,您没有任何需要解决的问题,因此不需要。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文