简体   繁体   中英

How to read a large size xml file in Java StaX Xml parser?

<Product>
<Row1>97545214</Row1>
<Row2>
  <value>01</value>
</Row2>
<Row4>
  <number>1</number>
  <role>A01</role>
  <name>Paul </name>
</Row4>
<Row4>
  <number>1</number>
  <role>A01</role>
  <name>James </name>
</Row4>
<Row4>
  <number>1</number>
  <role>A01</role>
  <name>John </name>
</Row4>
<Row5>
  <Code>01</Code>
  <Measurement>9.00</Measurement>
  <UnitCode>in</UnitCode>
</Row5>
<Row5>
  <Code>02</Code>
  <Measurement>6.00</Measurement>
  <UnitCode>in</UnitCode>
</Row5>
<Row5>
  <Code>03</Code>
  <Measurement>1.09</Measurement>
  <UnitCode>in</UnitCode>
</Row5>
<Row7>
  <price>
     <Code>01</PriceTypeCode>
     <Amount>62.95</Amount>
     <currency>USD</currency>
  </Price>
</Row7>
  <Row7>
  <price>
     <Code>01</PriceTypeCode>
     <Amount>62.95</Amount>
     <currency>USD</currency>
  </Price>

How to read this xml in java StaX parser. This is the sample xml. Original file size is more than 2 gb. so only i go for StaX parser. My Java class is BulkFileReader.java

public class BulkFileReader {


public static void main(String[] args) throws ClassNotFoundException, XMLStreamException, FileNotFoundException {

    String fileName = "E:\\Arunselvan\\D2 to D5\\xml files\\combine.xml";


    List<BookSpecBean> bookspec = (List<BookSpecBean>) parseXML(fileName);

    for(BookSpecBean bean : bookspec){
      System.out.println("The Row1="+bean.row1);
      System.out.println("The Row2="+bean.row2);
    System.out.println("The Number="+bean.number);
        System.out.println("The Role="+bean.role);
        System.out.println("The Name="+bean.name);
    System.out.println("The code="+bean.code);
        System.out.println("The amount="+bean.amount);
        System.out.println("The currency="+bean.currency);


      System.out.println("===========================================");

       new Query().InsertMetaData1(bean);
          }

    System.out.println("XML Completed Successfully");

}

private static List<BookSpecBean> parseXML(String fileName) {
    List<BookSpecBean> empList = new ArrayList<>();
    BookSpecBean emp = null;
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    try {
        XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName));

        while(xmlEventReader.hasNext()){
            XMLEvent xmlEvent = xmlEventReader.nextEvent();
           if (xmlEvent.isStartElement()){
               StartElement startElement = xmlEvent.asStartElement();


               if(startElement.getName().getLocalPart().equals("Product")){
                   emp = new BookSpecBean();

                   }
               }
               //set the other varibles from xml elements
               else if(startElement.getName().getLocalPart().equals("Row1")){
                   xmlEvent = xmlEventReader.nextEvent();
                   emp.setRow1(xmlEvent.asCharacters().getData());
               }
      else if(startElement.getName().getLocalPart().equals("Row2")){
                   xmlEvent = xmlEventReader.nextEvent();
                   emp.setRow2(xmlEvent.asCharacters().getData().replace("'", ""));
               }

               String qname = startElement.getName().getLocalPart();
               if(qname.equalsIgnoreCase("Row4")){

            xmlEvent = xmlEventReader.nextEvent();

        }
    else if(startElement.getName().getLocalPart().equals("number")){
        xmlEvent = xmlEventReader.nextEvent();
        emp.setnumber(xmlEvent.asCharacters().getData());
        }
    else if(startElement.getName().getLocalPart().equals("role")){
        xmlEvent = xmlEventReader.nextEvent();
        emp.setrole(xmlEvent.asCharacters().getData());
        }
    else if(startElement.getName().getLocalPart().equals("name")){
    xmlEvent = xmlEventReader.nextEvent();
    emp.setname(xmlEvent.asCharacters().getData());
        }

}
    if(xmlEvent.isEndElement()){
        EndElement endElement = xmlEvent.asEndElement();
        if(endElement.getName().getLocalPart().equals("Row4")){
        empList.add(emp);
    }
}

        else if(startElement.getName().getLocalPart().equals("code")){
            xmlEvent = xmlEventReader.nextEvent();
            emp.setcode(xmlEvent.asCharacters().toString());
        }
        else if(startElement.getName().getLocalPart().equals("Amount")){
            xmlEvent = xmlEventReader.nextEvent();
            emp.setamount(xmlEvent.asCharacters().getData());
        }
        else if(startElement.getName().getLocalPart().equals("currency")){
            xmlEvent = xmlEventReader.nextEvent();
            emp.setcurrency(xmlEvent.asCharacters().getData());
        }
           }
           //if Employee end element is reached, add employee object to list
           if(xmlEvent.isEndElement()){
               EndElement endElement = xmlEvent.asEndElement();
               if(endElement.getName().getLocalPart().equals("Product")){
                   empList.add(emp);
               }
           }

        }

    } catch (FileNotFoundException | XMLStreamException e) {
        e.printStackTrace();
    }
    return empList;
}

}

I use this java code to retrieve the values from the xml tag.When i use this code. i can able to retrieve the Third <row4> values. The remaining <row4> tag values cannot retrieve. Please help me to take the values from all the <row4> tag and <row7>

Thanks in advance for answering this question..

If you're using JaxB , you don't need to parse XML file by yourself , that's why JaxB is made for! :)


Basic steps to read-write xml using JaxB / Unmarshaller and XSD

  • Create a valid XSD file of your XML structure.
  • Place it in your project folder.
  • Right click XSD file and auto-generate JAXB classes .
  • Use Unmarshaller to populate auto-generated classes from XML file:

     JAXBContext jc = JAXBContext.newInstance(Product.class); String fileName = "E:\\\\Arunselvan\\\\D2 to D5\\\\xml files\\\\combine.xml"; Unmarshaller u = jc.createUnmarshaller(); Product product = (Product) u.unmarshal(new FileInputStream(filename)); 

That's it... JaxB will take care of classes, attributes, populate, write/read xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM