Saxを使用して大きなxmlドキュメントを解析しますが、何らかの条件が確立されたときにドキュメントの解析を停止したいですか?実行する方法?
18276 次
3 に答える
39
SAXExceptionのスペシャライゼーションを作成してスローします(独自のスペシャライゼーションを作成する必要はありませんが、具体的に自分でキャッチして、他のSAXExceptionを実際のエラーとして扱うことができます)。
public class MySAXTerminatorException extends SAXException {
...
}
public void startElement (String namespaceUri, String localName,
String qualifiedName, Attributes attributes)
throws SAXException {
if (someConditionOrOther) {
throw new MySAXTerminatorException();
}
...
}
于 2009-08-28T06:26:37.870 に答える
4
Tom によって概説された例外スロー技術以外に、SAX 解析を中止するメカニズムを知りません。別の方法として、 StAX パーサーの使用に切り替えることもできます (プルとプッシュを参照)。
于 2009-08-28T09:05:46.507 に答える
2
を使用したくないので、ブール変数 " stopParse
" を使用してリスナーを消費しますthrow new SAXException()
。
private boolean stopParse;
article.getChild("title").setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
if(stopParse) {
return; //if stopParse is true consume the listener.
}
setTitle(body);
}
});
アップデート:
@PanuHaaramo、この.xmlを持っていると仮定
<root>
<article>
<title>Jorgesys</title>
</article>
<article>
<title>Android</title>
</article>
<article>
<title>Java</title>
</article>
</root>
Android SAX を使用して「タイトル」値を取得するパーサーは、次のようにする必要があります。
import android.sax.Element;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
...
...
...
RootElement root = new RootElement("root");
Element article= root.getChild("article");
article.getChild("title").setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
if(stopParse) {
return; //if stopParse is true consume the listener.
}
setTitle(body);
}
});
于 2014-02-21T01:54:10.680 に答える