html - 使用 XmlSlurper 时出现 MalformedURLException

标签 html xml groovy xmlslurper

我在 groovy 中有以下代码片段:

s = '''
<html>
<head>
<title>My title</title>
</head>
<body>
This is body!
</body>
</html>'''

new XmlSlurper().parse(s)

它给出以下异常:

java.net.MalformedURLException: no protocol: 
<html>
<head>
<title>My title</title>
</head>
<body>
This is body!
</body>
</html>
    at java.net.URL.<init>(URL.java:586)
    at java.net.URL.<init>(URL.java:483)
    at java.net.URL.<init>(URL.java:432)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:613)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:812)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at groovy.util.XmlSlurper.parse(XmlSlurper.java:147)
    at groovy.util.XmlSlurper.parse(XmlSlurper.java:213)
    at groovy.util.XmlSlurper$parse.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at ConsoleScript12.run(ConsoleScript12:11)
    at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:257)
    at groovy.lang.GroovyShell.run(GroovyShell.java:481)
    at groovy.lang.GroovyShell.run(GroovyShell.java:163)
    at groovy.lang.GroovyShell$run.call(Unknown Source)
    at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy:955)
    at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
    at groovy.lang.Closure.call(Closure.java:411)
    at groovy.lang.Closure.call(Closure.java:405)
    at groovy.lang.Closure.run(Closure.java:492)
    at java.lang.Thread.run(Thread.java:744)

java.net.MalformedURLException: no protocol: 
<html>
<head>
<title>My title</title>
</head>
<body>
This is body!
</body>
</html>
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:613)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:812)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at ConsoleScript12.run(ConsoleScript12:11)

为什么在解析 XML 时会出现 MalformedURLException

最佳答案

当你解析一个字符串时,你需要做:

new XmlSlurper().parseText(s)

代替:

new XmlSlurper().parse(s)

Groovydocs :

GPathResult parse(String uri): Parse the content of the specified URI into a GPathResult Object.

关于html - 使用 XmlSlurper 时出现 MalformedURLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23359382/

相关文章:

java - 如何让 Button 右对齐,而不被 TextView 重叠?

Grails 域对象 hasMany 使用包含的不规则行为

amazon-web-services - 如何从config.groovy文件定义和访问grails中的数组

gradle - 如何排除Gradle的jar.from中的文件?

java - 如何只获取HTML页面的一部分?

javascript - Fancybox 内容颜色

html - CSS - 悬停链接时更改另一个元素的样式?

php - AJAX 与 HTML?

xml - Notepad++ XML Display on separate lines like Internet Explorer Display Of XML

xml - Jmeter- 如何从 HTTP 采样器的响应中读取 xml 属性值