0

I receive from Webservice the following XML:

<ns1:irsValorationReturn xmlns:ns1="http://webServices.es">
    <ns1:error xsi:nil="true"/>
    <ns1:format>dd/MM/yyyy</ns1:format>
    <ns1:pricingDate>02/09/2013</ns1:pricingDate>
    <ns1:results>
        <ns1:items>
           <ns1:bpv>-395.79435433174876</ns1:bpv>
           <ns1:npv>26960.051401523993</ns1:npv>
           <ns1:payingNpv>0.0</ns1:payingNpv>
           <ns1:pricingDelta>342.85778485967523</ns1:pricingDelta>
           <ns1:receivingNpv>26960.051401523993</ns1:receivingNpv>
           <ns1:swapRate>0.6811631117640043</ns1:swapRate>
        </ns1:items>
    </ns1:results>
</ns1:irsValorationReturn>

I need to transform this XML into a HashMap. The entries of HashMap must be variables depending on the value between keys {} ({KEY OF HASHMAP}) from the template below. The value of the corresponding key in Hashmap would be the original value from the WebService XML response, i.e.

BPV = -395.79435433174876; 
NPV = 26960.051401523993

...

<ns1:irsValorationReturn xmlns:ns1="http://webServices.es">
    <ns1:error xsi:nil="true"/>
    <ns1:format></ns1:format>
    <ns1:pricingDate><ns1:pricingDate>
    <ns1:results>
        <ns1:items>
            <ns1:bpv>{BPV}</ns1:bpv>
            <ns1:npv>{NPV}</ns1:npv>
            <ns1:payingNpv>{PAYING_NPV}</ns1:payingNpv>
            <ns1:pricingDelta>{PRICING_DELTA}</ns1:pricingDelta>
            <ns1:receivingNpv>{RECEIVING_NPV}</ns1:receivingNpv>
            <ns1:swapRate>{SWAP_RATE}</ns1:swapRate>
        </ns1:items>
    </ns1:results>
</ns1:irsValorationReturn>

If I change {NPV} for {NPV_NEW} the resulting HashMap should be:

BPV     = -395.79435433174876;
NPV_NEW = 26960.051401523993

I thought in parse template XML and when you find a tag with a value that matches with pattern {.+} search through the original XML for that tag. Then you put the entry in Hashmap with the key of the template XML and the value of the original XML.

Has any better way? I think it can be a little inefficient parsing original XML each time I find a node with variable value.

Alejandro Cuervo
  • 535
  • 1
  • 5
  • 16

1 Answers1

0

Did you check XStream?

Also this might be a duplicate of How to convert XML to java.util.Map and vice versa where they also talk about XStream.

Unless you have a huge XML reply I think XStream will be fast enough for parsing, and probably faster than parsing the XML yourself (This is however a guess since I don't know if you come up with a really clever way. :)).

Community
  • 1
  • 1
Qben
  • 2,617
  • 2
  • 24
  • 36
  • Thanks Qben but my problem is different from the referenced post because I need a previous transformation. Anyway I'll check for Xstream ;) – Alejandro Cuervo Sep 11 '13 at 07:22