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

1
  • You'd better format first your code; it does not even compile. Commented Oct 1, 2015 at 23:16

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The above XML i showed is the sample one. my file size is more than 3gb. If i use the JAXB it will throw some exception. The exception is:com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions @Jordi Castilla.
In my file i have more than one <Product> tag. Each Product tag contains same tag names. Exception is: Class has two properties of the same name "contributor" this problem is related to the following location: at public java.util.List com.main.File.Bean.Product.getContributor() at com.main.File.Bean.Product this problem is related to the following location: at private java.util.List com.main.File.Bean.Product.contributor at com.main.File.Bean.Product
Thats an xsd problem, not file size, create a valid xsd from your xml in any of the many online tools there are available
And unmarshall List<Product> or a parent class i.e.: Order

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.