2016-04-29 3 views
3
<root> 
<parent> 
    <child1> 30</child1> 
    <child2> 30</child2> 
    <child3> 30</child3> 
</parent> 
<parent> 
    <child1> 20</child1> 
    <child2> 30</child2> 
    <child3> 30</child3> 
</parent> 
<parent> 
    <child1> 30</child1> 
    <child2> 30</child2> 
    <child3> 30</child3> 
</parent> 
</root> 

を使用してXML解析の子タグの値に基づいて、親タグを、スキップするかをI必要があります。 ..タグchild1の値に基づいて、20より大きい場合、残りの子タグ(child2とchild3)を解析したい場合は、次の親タグに移動します。は、どのように私はコーディングの世界には本当に新しいですと、上記のXMLを考えてみましょう。..パース をサックスするSAXパーサ

誰もがそれを行うには理想的な方法が示唆できますか?

+0

なぜサックスだけですか?それは巨大なXMLのためですか? –

+0

はい、それは巨大なXMLのためのもので、すでに既存のコードですが、私はいくつかの変更をしようとしています。 –

+0

どのように巨大ですか? 100のmbまたは100のgb? –

答えて

1

そのような何か:以下

... 
private boolean skipChildren; 
private StringBuilder buf = new StringBuilder(); 
... 

@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 
    if (qName.equals("parent")) { 
     skipChildren = false; 
     ... 
    } else if (qName.equals("child1")) { 
     buf.setLength(0); 
     ... 
    } else if (qName.startsWith("child")) { 
     if (!skipChildren) { 
      buf.setLength(0); 
      ... 
     } 
    } 
} 

@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 
    if (qName.equals("parent")) { 
     ... 
    } else if (qName.equals("child1")) { 
     int value = Integer.parseInt(buf.toString().trim()); 
     if (value <= 20) { 
      skipChildren = true; 
     } 
     ... 
    } else if (qName.startsWith("child")) { 
     if (!skipChildren) { 
      int value = Integer.parseInt(buf.toString().trim()); 
      doSomethingWith(value); 
     } 
    } 
} 

@Override 
public void characters(char[] ch, int start, int length) { 
    if (!skipChildren) { 
     buf.append(ch, start, length); 
    } 
} 
+0

ありがとう@モーリスペリー。これはうまくいくように見えます。 –

0

vtd-xmlであなたのタスクを実行するためのコードですが、それは、XML処理技術で芸術の状態であり、そして多くの、より効率的かつSAXよりも書くことは簡単です。 ...キーは、可能な限り

Processing XML with Java – A Performance Benchmark

import com.ximpleware.*; 
public class conditionalSelection { 
    public static void main(String s[]) throws VTDException{ 
     VTDGen vg = new VTDGen(); 
     if(!vg.parseFile("d:\\xml\\condition.xml", false)) // disable namespace 
      return; 
     VTDNav vn = vg.getNav(); 
     AutoPilot ap = new AutoPilot(vn); 
     ap.selectXPath("/root/parent[child1>20]"); // the xpath selecting all parents with child1>20 
     int i=0,j=0; 
     while((i=ap.evalXPath())!=-1){ 
      // now move the cursor to child2 and child3 
      if(vn.toElement(VTDNav.FC,"child2")){ 
       j = vn.getText(); 
       if (j!=-1)//make sure the text node exist 
        System.out.println(" child2's text node is ==>"+ vn.toString(j)); 
       vn.toElement(VTDNav.P); 
      } 
      if(vn.toElement(VTDNav.FC,"child3")){ 
       j = vn.getText(); 
       if (j!=-1)//make sure the text node exist 
        System.out.println(" child3's text node is ==>"+ vn.toString(j)); 
       vn.toElement(VTDNav.P); 
      } 
     } 
    } 
0解析するSAXを避けるために、あなたの理由の負荷を与えること this paperを読む...興味のあるノードのみをフィルタリングするためにXPath式を使用することです
関連する問題