title: Why am I getting "MalformedURLException: no protocol" when using SAXParser?
body: <p>I'm copying code from one part of our application (an applet) to inside the app. I'm parsing XML as a String. It's been awhile since I parsed XML, but from the error that's thrown it looks like it might have to do with not finding the .dtd. The stack trace makes it difficult to find the exact cause of the error, but here's the message:</p>
  
  <pre><code>java.net.MalformedURLException: no protocol: &lt;a href="http://www.mycomp.com/MyComp.dtd"&gt;http://www.mycomp.com/MyComp.dtd&lt;/a&gt;
  </code></pre>
  
  <p>and the XML has this as the first couple lines:</p>
  
  <pre><code>&lt;?xml version='1.0'?&gt;
  &lt;!DOCTYPE MYTHING  SYSTEM '&lt;a href="http://www.mycomp.com/MyComp.dtd"&gt;http://www.mycomp.com/MyComp.dtd&lt;/a&gt;'&gt;
  </code></pre>
  
  <p>and here's the relevant code snippets</p>
  
  <pre><code>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);
      }
    }
  
  }
  </code></pre>
  
  <p>It looks like the error is happening in the preparseString() method, on the line that actually does the parsing, the <code>m_SaxParser.parse(lSource, this);</code> line.</p>
  
  <p>FYI, the 'MyComp.dtd' file does exist at that location and is accessible via http. The XML file comes from a different service on the server, so I can't change it to a file:// format and put the .dtd file on the classpath. </p>