Arunselvan Arunselvan - 8 months ago 32
Java Question

How to read the same xml tag using Stax parser


I want the output as

Pricetypecode : 01
PriceAmount : 29.95
Currencycode : INR

Pricetypecode : 08
Price Amount : 14.32
Currencycode : INR

import com.main.Query.Query;
import com.main.Bean.PriceDetail;
import com.main.Bean.SpecificationBook;
import java.sql.SQLException;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;

public static void main(String[] args) throws ClassNotFoundException, XMLStreamException, FileNotFoundException, SQLException {
String fileName = "D:\\SOFTWARE\\txt20160401.xml";
List<SpecificationBook> bookspec = (List<SpecificationBook>) parseXML(fileName);
for(SpecificationBook bean : bookspec){
System.out.println("The Price type code 08="+bean.priceTypeCode);
System.out.println("The price amount 08 is:"+bean.priceAmount);
System.out.println("The Currency Code 08 is:"+bean.currencyCode);
System.out.println("The price typecodechar 01 is:"+bean.priceTypeCodeChar);
System.out.println("The price amount 01 is:"+bean.priceAmount2);
System.out.println("The Currency code 01 is:"+bean.currencyCode1);
private static List<SpecificationBook> parseXML(String fileName) throws ClassNotFoundException, XMLStreamException {
List<SpecificationBook> empList = new ArrayList<>();
SpecificationBook emp = null;

XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
try {
XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName));

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

emp = new SpecificationBook();
else if(startElement.getName().getLocalPart().equals("PriceTypeCode")){
xmlEvent = xmlEventReader.nextEvent();

xmlEvent = xmlEventReader.nextEvent();
else if(startElement.getName().getLocalPart().equals("CurrencyCode")){
xmlEvent = xmlEventReader.nextEvent();
EndElement endElement = xmlEvent.asEndElement();


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

My Pojo class.

public class SpecificationBook {

@Getter @Setter public String recordReference;
@Getter @Setter public String titleText;
@Getter @Setter public String imprintName;
@Getter @Setter public String publisherName;
@Getter @Setter public String illustrationDesc;
@Getter @Setter public String noofPages;
@Getter @Setter public String priceTypeCode;
@Getter @Setter public int book_id;
@Getter @Setter public String editionversionnumber;
@Getter @Setter public String recordreference1;
@Getter @Setter public String recordreference2;
@Getter @Setter public String recordreference3 ;
@Getter @Setter public String priceTypeCodeChar;
@Getter @Setter public String priceAmount;
@Getter @Setter public String priceAmount2;
@Getter @Setter public String currencyCode;
@Getter @Setter public String currencyCode1;

I tried to get the value from the supply detail
tag values. While run this code. i got the values from the second
tag values only.

While run this code in my machine I got the output as

The Price type code 08=08
The price amount 08 is:14.83
The Currency Code 08 is:INR
The price typecodechar 01 is:null
The price amount 01 is:null
The Currency code 01 is:null


You never invoke the setters for priceTypeCodeChar, priceAmount2 and currencyCode1 - so those will always print null.

Additionally, SpecificationBook is created for each Product tag and added to the result list when you encounter the Product's close tag. This way the values will be overwritten for every new Price.

The easiest way to fix this would be to change your SpecificationBook class, remove the Price specific fields and add a list of Price objects.

class SpecificationBook {
    // other fields
    List<Price> prices;

where the Price class would look something like

class Price {
    String priceAmount;
    String priceTypeCode;
    String currencyCode;

You could then check the start-tags for Price, add the details and, when you encounter the Price end-tag, add it to the SpecificationBook's prices.