使用SAXParser时为什么会出现"MalformedURLException:no protocol"?

use*_*270 6 java xml saxparser

我正在将代码从应用程序的一部分(applet)复制到应用程序内部.我正在将XML解析为String.自从我解析XML以来已经有一段时间了,但是从抛出的错误看起来它可能与找不到.dtd有关.堆栈跟踪使得很难找到错误的确切原因,但这里是消息:

java.net.MalformedURLException: no protocol: <a href="http://www.mycomp.com/MyComp.dtd">http://www.mycomp.com/MyComp.dtd</a>
Run Code Online (Sandbox Code Playgroud)

并且XML将此作为第一对行:

<?xml version='1.0'?>
<!DOCTYPE MYTHING  SYSTEM '<a href="http://www.mycomp.com/MyComp.dtd">http://www.mycomp.com/MyComp.dtd</a>'>
Run Code Online (Sandbox Code Playgroud)

这是相关的代码片段

class XMLImportParser extends DefaultHandler {

  private SAXParser m_SaxParser = null;
  private String is_InputString = "";

  XMLImportParser(String xmlStr) throws SAXException, IOException {
    super();
    is_InputString = xmlStr;
    createParser();
    try {
      preparseString();
      parseString(is_InputString);
    } catch (Exception e) {
       throw new SAXException(e); //"Import Error : "+e.getMessage());
    }
  }

  void createParser() throws SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    try {
        factory.setFeature("http://xml.org/sax/features/namespaces", true);
        factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        m_SaxParser = factory.newSAXParser();
        m_SaxParser.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", true);
        m_SaxParser.getXMLReader().setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    } catch (SAXNotRecognizedException snre){
        throw new SAXException("Failed to create XML parser");  
    } catch (SAXNotSupportedException snse) {
        throw new SAXException("Failed to create XML parser");  
    } catch (Exception ex) {
        throw new SAXException(ex);  
    }
  }

  void preparseString() throws SAXException {
    try {
        InputSource lSource = new InputSource(new StringReader(is_InputString));
        lSource.setEncoding("UTF-8");
        m_SaxParser.parse(lSource, this);
    } catch (Exception ex) {
        throw new SAXException(ex);
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

看起来错误发生在preparseString()方法中,在实际执行解析的m_SaxParser.parse(lSource, this);行上,即行.

仅供参考,"MyComp.dtd"文件确实存在于该位置,可通过http访问.XML文件来自服务器上的不同服务,因此我无法将其更改为file://格式并将.dtd文件放在类路径上.

Joh*_*man 6

我认为您在 XML 声明中有一些额外的代码。尝试这个:

<?xml version='1.0'?>
<!DOCTYPE MYTHING  SYSTEM "http://www.mycomp.com/MyComp.dtd">
Run Code Online (Sandbox Code Playgroud)

以上内容来自 W3C 建议:http : //www.w3.org/QA/2002/04/valid-dtd-list.html

在创建解析器之前,您可以使用 http 链接在 SAXParserFactory 上设置架构。

void createParser() throws SAXException {
    Schema schema = SchemaFactory.newSchema(new URL("http://www.mycomp.com/MyComp.dtd"));
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    factory.setSchema(schema);
Run Code Online (Sandbox Code Playgroud)