简体   繁体   English

为什么此代码会引发MalformedURLException?

[英]Why does this code throw a MalformedURLException?

I have the following code snippet: 我有以下代码片段:

public class DOMTest {

    public static void main( String[] args )
            throws ParserConfigurationException, SAXException, IOException {
        String xml = "<D:propfind xmlns:D=\"DAV:\""
                + " xmlns:C=\"urn:ietf:params:xml:ns:carddav\">\n"
                + "  <D:prop>\n" + "    <D:displayname />\n"
                + "    <C:getctag />\n" + "  </D:prop>\n" + "</D:propfind>";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware( true );
        DocumentBuilder builder = null;
        Document doc = null;
        builder = factory.newDocumentBuilder();
        System.out.println( "namespaceAware=" + builder.isNamespaceAware()
                + ", validating=" + builder.isValidating() );
        doc = builder.parse( xml );
    }
}

Running it produces the following output: 运行它会产生以下输出:

namespaceAware=true, validating=false
Exception in thread "main" java.net.MalformedURLException: no protocol:
<D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav
  <D:prop>
    <D:displayname />
    <C:getctag />
  </D:prop>
</D:propfind>
at java.net.URL.<init>(URL.java:593)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:620)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:805)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
at com.lemcke.c.dav.card.DOMTest.main(DOMTest.java:31)

The xml is a standard example for a CardDAV request. xml是CardDAV请求的标准示例。 Is this some configuration problem when using namespaces? 使用名称空间时,这是一些配置问题吗?

The parse method can take an InputStream as a paramtere or a String, if you give it a string it will try to open the location of the string and in your case the string is the content. parse方法可以将InputStream用作参数或字符串,如果给它一个字符串,它将尝试打开字符串的位置,在这种情况下,字符串就是内容。

Here is how you can fix this: 解决方法如下:

    doc = builder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

You can find more information here: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html 您可以在此处找到更多信息: http : //docs.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html

This is my code snippet which works for me: 这是适合我的代码段:

Document doc;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(website);

String website contains a correct URL of XML, for example: https://www.w3schools.com/xml/note.xml String website包含正确的XML URL,例如: https : //www.w3schools.com/xml/note.xml

If you want use String directly, you should try: 如果要直接使用String,则应尝试:

InputSource inputStream = new InputSource(new StringReader(xml));
return builder.parse(inputStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM