java - How to read the same xml tag using Stax parser -


<product> <supplydetail> <price>  <pricetypecode>01</pricetypecode>   <discountcoded>     <discountcodetype>02</discountcodetype>                    <discountcodetypename>lsi</discountcodetypename>     <discountcode>25</discountcode></discountcoded>  <priceamount>29.95</priceamount>  <currencycode>inr</currencycode> </price> </supplydetail> <supplydetail> <price>   <pricetypecode>08</pricetypecode>   <priceamount>14.32</priceamount>   <currencycode>inr</currencycode> </price> </supplydetail> </product> 

i want output

pricetypecode  : 01 priceamount  : 29.95 currencycode  : inr  pricetypecode : 08 price amount  : 14.32 currencycode  : inr 

bulkfilexmlreader2.java

import com.main.query.query; import com.main.bean.pricedetail; import com.main.bean.specificationbook; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.sql.sqlexception; import java.text.normalizer; import java.util.arraylist; import java.util.list; import javax.xml.stream.xmleventreader; import javax.xml.stream.xmlinputfactory; import javax.xml.stream.xmlstreamexception; import javax.xml.stream.events.endelement; import javax.xml.stream.events.startelement; import javax.xml.stream.events.xmlevent;  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));     while(xmleventreader.hasnext()){             xmlevent xmlevent = xmleventreader.nextevent();            if (xmlevent.isstartelement()){                startelement startelement = xmlevent.asstartelement(); if(startelement.getname().getlocalpart().equals("product")){                     emp = new specificationbook();                    } else if(startelement.getname().getlocalpart().equals("pricetypecode")){                    xmlevent = xmleventreader.nextevent();  emp.setpricetypecode(xmlevent.ascharacters().tostring());                }                 else    if(startelement.getname().getlocalpart().equals("priceamount")){ xmlevent = xmleventreader.nextevent(); emp.setpriceamount(xmlevent.ascharacters().tostring()); } else if(startelement.getname().getlocalpart().equals("currencycode")){ xmlevent = xmleventreader.nextevent(); emp.setcurrencycode(xmlevent.ascharacters().tostring());                }            } if(xmlevent.isendelement()){                endelement endelement = xmlevent.asendelement();  if(endelement.getname().getlocalpart().equals("product")){ emplist.add(emp);                }            }          }  } catch (filenotfoundexception | xmlstreamexception e) {          e.printstacktrace();      }      return emplist;  } 

my pojo class. specificationbook.java

  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 value supply detail <price> tag values. while run code. got values second <supplydetail> tag values only.

while run code in machine got output as

the price type code 08=08 price amount 08 is:14.83 currency code 08 is:inr  price typecodechar 01 is:null price amount 01 is:null currency code 01 is:null 

you never invoke setters pricetypecodechar, priceamount2 , currencycode1 - print null.

additionally, specificationbook created each product tag , added result list when encounter product's close tag. way values overwritten every new price.

the easiest way fix change specificationbook class, remove price specific fields , add list of price objects.

class specificationbook {     // other fields     list<price> prices; } 

where price class like

class price {     string priceamount;     string pricetypecode;     string currencycode; } 

you check start-tags price, add details and, when encounter price end-tag, add specificationbook's prices.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -