parsing large XML using SAX in java

后端 未结 4 2015
天命终不由人
天命终不由人 2021-01-22 19:53

I am trying to parse the stack overflow data dump, one of the tables is called posts.xml which has around 10 million entry in it. Sample xml:



        
4条回答
  •  离开以前
    2021-01-22 20:07

    "StartElement" Sax Event permits to process a single XML ELement.

    In java code you must implement this method

    public void startElement(String uri, String localName,
        String qName, Attributes attributes)
        throws SAXException {
    
        if("row".equals(localName)) {
            //this code is executed for every xml element "row"
            String id = attributes.getValue("id");
            String PostTypeId = attributes.getValue("PostTypeId");
            String AcceptedAnswerId = attributes.getValue("AcceptedAnswerId");
            //others two
            // you have your att values for an "row" element
        }
    
     }
    

    For every element, you can access:

    1. Namespace URI
    2. XML QName
    3. XML LocalName
    4. Map of attributes, here you can extract your two attributes...

    see ContentHandler Implementation for specific deatils.

    bye

    UPDATED: improved prevous snippet.

提交回复
热议问题