-1

I call method of another service and I recieve this kind of xml string:

<result state="0" code="04998121129111115327" transID="4444"
        transTime="2012-11-29 11:11:14" regCode="040020275"
        refCode="DZ053158110" meterNum="02003561710"
        customerName="Jorsh Bush" tariffCode="02"
        buyTimes="97" calcQty="37.4" vendQty="37.4"
        vendAMT="9.7" supplyAMT="0.00" arrearAMT="0.00"
        feeAMT="0" AMT="9.7" VAT="0" stampTax="0.00"
        netAMT="9.7" commAMT="0.3" token="2761 9986 4217 4379 4463"
        invoice="0000000544" verifyCode="6777e5da18c133d725c7dcd4153f5ca4" 
        checkCode="">
    <power>
        <item id="1" kwh="0" amt="0" price="0.1500"/>
        <item id="2" kwh="37.4" amt="9.7" price="0.2600"/>
    </power>
    <fee/>
    <arrear/>
</result>

This type of xml seems to me problematic, because data I need is in attrubutes(not in tags). Now, what is the best way to fit that kind of stuff into my java object?

I use xml schema to generate response class into which I try to convert xml string.

D. Zeliko
  • 1
  • 3
  • Don't reinvent the wheel. Use [JAXB](https://docs.oracle.com/javase/tutorial/jaxb/intro/). – Kartik Feb 08 '19 at 04:10
  • Yes , but How JAXB can convert that kind of xml string into java object? – D. Zeliko Feb 08 '19 at 04:24
  • There are millions of tutorials online. The attributes will go into Java fields annotated with `@XmlAttribute` and the tag values will go into `@XmlElement` fields. You'll have to do the work on your own and ask specific questions if you get stuck. – Kartik Feb 08 '19 at 04:31
  • Okey, I understand. Thank you for your reply. – D. Zeliko Feb 08 '19 at 04:56

1 Answers1

0

Ref: https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Unmarshaller.html

Like the first person said, use the Unmarshaller provided by JAXB

Should look something like this:
     JAXBContext jc = JAXBContext.newInstance( "com.acme.foo");   
     Unmarshaller u = jc.createUnmarshaller();   
     Object o = u.unmarshal(new File( "nosferatu.xml" ));
James
  • 11
  • 5