2015-06-10 9 views
13

Sto tentando di copiare un elemento in un report di copertura HTML, quindi i totali di copertura vengono visualizzati nella parte superiore del report e nella parte inferiore.Come aggirare l'XmlSlurper di Groovy rifiutando di analizzare l'HTML a causa delle restrizioni DOCTYPE e DTD?

Il codice HTML inizia così e credo sia ben formato:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
    <link rel="stylesheet" href=".resources/report.css" type="text/css" /> 
    <link rel="shortcut icon" href=".resources/report.gif" type="image/gif" /> 
    <title>Unified coverage</title> 
    <script type="text/javascript" src=".resources/sort.js"></script> 
    </head> 
    <body onload="initialSort(['breadcrumb', 'coveragetable'])"> 

XmlSlurper Groovy lamenta come segue: DOCTYPE

doc = new XmlSlurper(/* false, false, false */).parse("index.html") 
[Fatal Error] index.html:1:48: DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to true. 
DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to true. 

Abilitazione:

doc = new XmlSlurper(false, false, true).parse("index.html") 
[Fatal Error] index.html:1:148: External DTD: Failed to read external DTD 'xhtml1-strict.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property. 
External DTD: Failed to read external DTD 'xhtml1-strict.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property. 

doc = new XmlSlurper(false, true, true).parse("index.html") 
[Fatal Error] index.html:1:148: External DTD: Failed to read external DTD 'xhtml1-strict.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property. 
External DTD: Failed to read external DTD 'xhtml1-strict.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property. 


doc = new XmlSlurper(true, true, true).parse("index.html") 
External DTD: Failed to read external DTD 'xhtml1-strict.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property. 

doc = new XmlSlurper(true, false, true).parse("index.html") 
External DTD: Failed to read external DTD 'xhtml1-strict.dtd', because 'http' access is not allowed due to restriction set by the accessExternalDTD property. 

quindi penso Ho coperto tutte le opzioni. Ci deve essere un modo per farlo funzionare senza ricorrere a espressioni regolari e rischiare l'ira di Tony The Pony.

risposta

29

Tsk.

parser=new XmlSlurper() 
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false) 
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
parser.parse(it) 
1

Anche se il codice HTML avviene anche per essere XML ben formato, una soluzione più generale per il parsing HTML è usare un vero parser HTML. Ho usato il parser TagSoup in passato e gestisce abbastanza bene l'HTML reale.

TagSoup fornisce un parser che implementa l'interfaccia javax.xml.parsers.SAXParser e può essere fornito a XmlSlurper nel costruttore. Esempio:

@Grab('org.ccil.cowan.tagsoup:tagsoup:1.2.1') 

import org.ccil.cowan.tagsoup.Parser 

def doc = new XmlSlurper(new Parser()).parse("index.html") 
Problemi correlati