2016-04-29 23 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> 

Ich bin wirklich neu in der Welt der Codierung überspringen und Parsen Sax .. die oben XML Betrachten, was ich Bedürfnis ist. .. basierend auf dem Wert des Tags child1, wenn es größer als 20 ist, nur dann würde ich die verbleibenden Kind-Tags (child2 und child3) analysieren wollen, sonst würde ich mit dem nächsten Eltern-Tag weitermachen wollen.Wie der übergeordneten Tag, basierend auf dem Wert eines Kindes-Tages im XML-Parsing mit SAX-Parser

Könnte jemand bitte vorschlagen, was wäre der ideale Weg, es zu tun?

+0

Warum nur sax ist es für riesiges XML? –

+0

Ja, es ist für große xml, und es ist auch ein bereits vorhandener Code, ich versuche, einige Änderungen vorzunehmen. –

+0

Wie groß ist riesig? 100er MB oder 100 GB? –

Antwort

1

So etwas:

... 
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

Danke @Maurice Perry. Sieht so aus, als würde das funktionieren. –

0

Unten ist der Code Ihre Aufgabe mit vtd-xml auszuführen, ist es der Stand der Technik in XML-Verarbeitungstechnologie, und ist viel effizienter und einfacher als SAX zu schreiben. .. ist der Schlüssel von XPath-Ausdruck nur mit den Knoten von Interesse herauszufiltern ... lesen this paper, dass Sie viele Gründe gibt SAX-Parsing zu vermeiden, wann immer möglich

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); 
      } 
     } 
    }