Note that there are some explanatory texts on larger screens.

plurals
  1. POGroovy XMLSlurper issue
    primarykey
    data
    text
    <p>I want to parse with XmlSlurper a HTML document which I read using HTTPBuilder. Initialy I tried to do it this way:</p> <pre><code>def response = http.get(path: "index.php", contentType: TEXT) def slurper = new XmlSlurper() def xml = slurper.parse(response) </code></pre> <p>But it produces an exception: </p> <pre><code>java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd </code></pre> <p>I found a workaround to provide cached DTD files. I found a simple implementation of class which should help <a href="http://blog.bensmann.com/http-503-when-parsing-xml" rel="nofollow noreferrer">here</a>:</p> <pre><code>class CachedDTD { /** * Return DTD 'systemId' as InputSource. * @param publicId * @param systemId * @return InputSource for locally cached DTD. */ def static entityResolver = [ resolveEntity: { publicId, systemId -&gt; try { String dtd = "dtd/" + systemId.split("/").last() Logger.getRootLogger().debug "DTD path: ${dtd}" new org.xml.sax.InputSource(CachedDTD.class.getResourceAsStream(dtd)) } catch (e) { //e.printStackTrace() Logger.getRootLogger().fatal "Fatal error", e null } } ] as org.xml.sax.EntityResolver } </code></pre> <p>My package tree looks as shown below:</p> <p><img src="https://i.stack.imgur.com/1gqF9.jpg" alt="alt text"></p> <p>I modified also a little code for parsing response, so it looks like this:</p> <pre><code>def response = http.get(path: "index.php", contentType: TEXT) def slurper = new XmlSlurper() slurper.setEntityResolver(org.yuri.CachedDTD.entityResolver) def xml = slurper.parse(response) </code></pre> <p>But now I'm getting <code>java.net.MalformedURLException</code>. Logged DTD path from CachedDTD entityResolver is <code>org/yuri/dtd/xhtml1-transitional.dtd</code> and I can't get it working...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload