0

I'm having fun with a weird xml response I get - the xml:

<params>
    <param>
        <value><array><data>
            <value><string>UstId_1</string></value>
            <value><string>xxx</string></value>
        </data></array></value>
    </param>
    <param>
        <value><array><data>
            <value><string>ErrorCode</string></value>
            <value><string>200</string></value>
        </data></array></value>
    </param>
</params>

Basically, the most inner <value><string> construct would normally be

<UstId_1>xxx</UstId_1>

and

<ErrorCode>200</ErrorCode>

respectively, so that the message of the xml boils down to

<params>
    <UstId_1>xxx</UstId_1>
    <ErrorCode>200</ErrorCode>
</params>

But this xml is different. It's returned by a tax authority, so there's no way to make them change that.

I currently have this pojo

@JacksonXmlRootElement(localName = "params")
public class Params {
    @JacksonXmlProperty(localName = "param")
    private List<Param> paramList = new ArrayList<>();

    //getter setter ...
}

and Param:

public class Param {
    @JacksonXmlProperty(localName = "value")
    private Object value;
    @JacksonXmlProperty(localName = "array")
    private Object array;
    @JacksonXmlProperty(localName = "data")
    private Object data = new ArrayList<>();
    //getter setter....
}

But that does only return the second entry from <value><string>, e.g.

xxx

and

200

Also it's a very strange construct

Params{paramList=[Param{value={array={data={value={string=xxx}}}}, array=null, data=null}
...

How would I correctly set up a pojo for that xml to ideally be able to do

res.getUstId1();
baao
  • 71,625
  • 17
  • 143
  • 203

1 Answers1

3

Maybe not what you were aiming for, but would an XSLT help you? You could transform the XML into something you can easily parse. Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/">
    <params>
      <xsl:for-each select="/params/param/value/array/data">
        <xsl:element name="{value[1]/string}">
          <xsl:value-of select="value[2]/string"/>
        </xsl:element>
      </xsl:for-each>
    </params>
  </xsl:template>
</xsl:stylesheet>

Fiddle: http://xsltransform.net/ejivdHU

fafl
  • 7,222
  • 3
  • 27
  • 50
  • I've never done this before, but it looks interesting. Can you explain how to use that in java? – baao Feb 21 '17 at 09:48
  • Basically you transform XML into a different structure. You would have to run it on the response from the tax authority before parsing it into objects. I'm not sure how your stack looks like, so maybe these answers will help you: http://stackoverflow.com/questions/4604497/xslt-processing-with-java – fafl Feb 21 '17 at 09:51