2

XmlSlurper を使用してダーティ html ページを解析すると、次のエラーが発生します。

ERROR org.xml.sax.SAXParseException: Element type "scr" must be followed by either attribute specifications, ">" or "/>".
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        ...
[Fatal Error] :1157:22: Element type "scr" must be followed by either attribute specifications, ">" or "/>".

今、私はそれをフィードし、そうする前にそれを印刷するhtmlを持っています。それを開いて、エラー 1157 に記載されている行に移動しようとすると、そこに「src」はありません (ただし、ファイルにはそのような文字列が何百もあります)。したがって、行番号を変更する追加のものが挿入されていると思います(おそらく<script>またはそのようなもの)。

問題のある行または html 部分を正確に見つける良い方法はありますか?

4

2 に答える 2

0

どのSAXParserを使用していますか?HTMLは厳密なXMLではないため、デフォルトのパーサーでXMLSlurperを使用すると、エラーが継続する可能性があります。

「Groovyhtmlslurper」をグーグルで大まかに検索すると、TagSoupと呼ばれるSaxParserを指すGroovyを使用したHTMLスクレイピングが表示されました。

それを回転させて、ダーティページを解析するかどうかを確認します。

于 2012-01-05T17:22:44.307 に答える
0

_lineNum という名前の属性を各要素に追加して、使用できるようにすることができます。

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.Attributes2Impl;
import javax.xml.parsers.ParserConfigurationException;

class MySlurper extends XmlSlurper {    
    public static final String LINE_NUM_ATTR = "_srmLineNum"
    Locator locator

    public MySlurper() throws ParserConfigurationException, SAXException {
        super();
    }

    @Override
    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
        Attributes2Impl newAttrs = new Attributes2Impl(attrs);        
        newAttrs.addAttribute(uri, LINE_NUM_ATTR, LINE_NUM_ATTR, "ENTITY", "" + locator.getLineNumber());        
        super.startElement(uri, localName, qName, newAttrs);
    }
}

def text = '''
<root>
  <a>one!</a>
  <a>two!</a>
</root>'''

def root = new MySlurper().parseText(text)

root.a.each { println it.@_srmLineNum }

上記は line num 属性を追加します。おそらく、ロケーターから行番号を読み取ることができる独自のエラー ハンドラーを設定することができます。

于 2016-05-31T10:22:37.993 に答える