0

I am using spring rest template to interact with a rest service written using wcf (consuming xml).

I have an entity class called Rule having common fields and a child class CustomRule with some specific fields.

WCF service is expecting xml elements in alphabetical order, I tried using @XmlAccessorOrder on my base and child class but generated xml has base class fields in sorted order and child class fields in sorted order, but all elements in xml are not sorted

i.e. If Rule has fields S,A,B

CustomRule has Z,C

I expect A,B,C,S,Z order in xml,

instead I get A,B,S,C,Z.

Can someone please help?

Also is it possible to order xml elements based on value in XmlElement type instead varibale name in class ?

coder
  • 4,458
  • 2
  • 17
  • 23

1 Answers1

0

You cannot order within XML, but you can do a xslt transformation from your xml: Transform From one JAXB object to another using XSLT template

When you write your xslt templat you can apply the sort tag for elements that you want to sort: Sort XML nodes in alphabetical order using XSL

XSLT example sorting by name:

<xsl:template match="element_list">
    <xsl:apply-templates select="elements" />
</xsl:template>

<xsl:template match="elements">
    <table>
   <tbody>
          <xsl:apply-templates select="element">
             <xsl:sort select="name" />
          </xsl:apply-templates>
       </tbody>
    </table>
</xsl:template>

The XML file:

<element_list>
    <element>
         <name>Foo</name>
    </element>
    <element>
         <name>Bar</name>
    </element>
    <element>
         <name>WWW</name>
    </element>
    <element>
         <name>AAA</name>
    </element>
</element_list>

You'll get a table with following order: AAA, Bar, Foo, WWW

Community
  • 1
  • 1
carlosvin
  • 979
  • 9
  • 22