sorting xml

classic Classic list List threaded Threaded
2 messages Options
js
Reply | Threaded
Open this post in threaded view
|

sorting xml

js


trying to create CodeType and child Code classes from this xml with Codes in alphabetical order based upon <codeValue>

<codeType>
  <codeTypeValue>AddressType</codeTypeValue>
 
- <codes>
-   <code>
    <codeValue>B</codeValue>
    <description>Business</description>   
 </code>

-   <code>
    <codeValue>CON</codeValue>
    <description>Confidential Address</description> 
  </code>
</codes>
</codeType>



        CodeType codeType = new CodeType();
        XStream xstream = new XStream(new StaxDriver());      
        SortableFieldKeySorter sorter = new SortableFieldKeySorter();
        sorter.registerFieldOrder(Code.class, new String[] { "codeValue" });
        xstream = new XStream(new PureJavaReflectionProvider(new FieldDictionary(sorter)));

        try {
            xstream.alias("codeType", CodeType.class);
            xstream.alias("code", Code.class);
            codeType = (CodeType)xstream.fromXML(xml.getInputStream());
        }
        catch { ... }

jars:
xstream-1.4.7.jar
xpp3_min-1.1.4c.jar 
xmlpull-1.1.3.1.jar

if i comment out:
xstream = new XStream(new PureJavaReflectionProvider(new FieldDictionary(sorter)));

it works.

otherwise, all is null in CodeType

whats the most basic way to enable sorting in Xstream?

-thx



Reply | Threaded
Open this post in threaded view
|

Re: sorting xml

Jörg Schaible-4
Hi,

js wrote:

> trying to create CodeType and child Code classes from this xml with Codes
> in alphabetical order based upon <codeValue>

You can't. That's not what a FieldKeySorter is for. Such an implementation
defines the sequence of the fields (incl. inherited) of a specific type i.e.
you can "sort" "codeValue" and "description" for the members of Code.class.
However, a SortableFieldKeySorter requires the defined sequence for *all*
fields of the type, otherwise it complains with a StreamException.

>
> <codeType>
>    <codeTypeValue>AddressType</codeTypeValue>
>
>  -
>
<file:///C:/myFiles/sustain/documentation/conversion/newDawn/xmlCodeFiles/codes/AddressType.xml#>
> <codes>
>  -
>
<file:///C:/myFiles/sustain/documentation/conversion/newDawn/xmlCodeFiles/codes/AddressType.xml#>
>  <code>
>      <codeValue>B</codeValue>
>      <description>Business</description>
>  </code>
>
>  -
>
<file:///C:/myFiles/sustain/documentation/conversion/newDawn/xmlCodeFiles/codes/AddressType.xml#>

>  <code>
>      <codeValue>CON</codeValue>
>      <description>Confidential Address</description>
>   </code>
> </codes>
> </codeType>
>
>
>
>         CodeType codeType = new CodeType();
>         XStream xstream = new XStream(new StaxDriver());
>         SortableFieldKeySorter sorter = new SortableFieldKeySorter();
>         sorter.registerFieldOrder(Code.class, new String[] { "codeValue"
>         }); xstream = new XStream(new PureJavaReflectionProvider(new
> FieldDictionary(sorter)));
>
>         try {
>             xstream.alias("codeType", CodeType.class);
>             xstream.alias("code", Code.class);
>             codeType = (CodeType)xstream.fromXML(xml.getInputStream());
>         }
>         catch { ... }
>
> jars:
> xstream-1.4.7.jar
> xpp3_min-1.1.4c.jar
> xmlpull-1.1.3.1.jar
>
> if i comment out:
> xstream = new XStream(new PureJavaReflectionProvider(new
> FieldDictionary(sorter)));
>
> it works.
>
> otherwise, all is null in CodeType
>
> whats the most basic way to enable sorting in Xstream?


Apart from the sequence of fields, XStream does not sort its output. You may
apply a stylesheet to perform the sorting in a post-processing step:
http://xstream.codehaus.org/manual-tweaking-output.html#Procesing_XSLT

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email