views:

219

answers:

1

Basically, I want to do the reverse of this question.

I'm getting XML from Microsoft's Bing batch Geocode service, and some of the elements look like this (poached from here):

<DataflowJob>
    <Id>5bf10c37df944083b1879fbb0556e67e</Id>
    <Link role="self">https://spatial.virtualearth.net /REST/v1/dataflows/Geocode/5bf10c37df944083b1879fbb0556e67e</Link>
    <Link role="output" name="succeeded">https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/5bf10c37df944083b1879fbb0556e67e/output/succeeded&lt;/Link&gt;
    <Link role="output" name="failed">https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/5bf10c37df944083b1879fbb0556e67e/output/failed&lt;/Link&gt;
    <Description>Xml</Description>
    <Status>Completed</Status>
    ...
</DataflowJob>

Notice that the <Link> elements have attributes as well as text content. Here are the relevant POJO classes I'm trying to deserialize to:

class DataflowJob
{
    String Id;
    @XStreamImplicit
    List<Link> Links;
    String Description;
    Status Status;
    ...
}

class Link
{
    @XStreamAsAttribute
    Role role;
    @XStreamAsAttribute
    Name name;
    String url;
}

With my current configuration (classes are aliased, attributes auto-detected, and all that jazz), XStream properly deserializes the Name and Role attributes on the <Link> elements, but not the actual link text itself.

How do I get XStream to deserialize that text into a String field in a Link object?

I don't want to have to manually insert new elements around the link text* just for this.


*e.g., replace

<Link role="self">
    https://long/url/here
</Link>

with

<Link role="self">
    <url>https://long/url/here&lt;/url&gt;
</Link>
+1  A: 

XStream isn't a suitable tool for this. XStream's emphasis is on serializing/deserializing arbitrary java objects graphs to XML, rather than serializing/deserializing arbitrary XML.

It's going to be an uphill fight bending XStream to your will. I recommend using something better designed for this task, such as JAXB (built into JavaSE6) or JiBX.

skaffman
Dang. If that's truly the case, guess I'll go with the nasty hack I footnoted.
Matt Ball