Parsing GPX XML "any"-Element from XSD generated classes

71 views Asked by At

I'm trying to parse Geocaching GPX files with java. Those GPX files are based on standard GPX XSD file with an additional XSD file.

For class generation I'm using jaxb-maven-plugin.

Except from pom.xml:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <id>xjc</id>
                <goals>
                    <goal>xjc</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <packageName>de.darkspirit510.gpxparser</packageName>
        </configuration>
    </plugin>
</plugins>

XSD-Files:

My problem: The base XSD defines an any attribute to extend with another XSD. When unmashalling my GPX file (using all relevant classes and ObjectFactory) with the following code snippet:

Gpx gpx = (Gpx) JAXBContext
            .newInstance(Gpx.class, Cache.class, ObjectFactory.class)
            .createUnmarshaller()
            .unmarshal(inputStream);

and an GPX file (except)

<wpt lat="52.171967" lon="10.55625">
    <time>2015-07-14T07:00:00Z</time>
    <name>GC5ZA4J</name>
    <desc>Bücherei WF-Nordost by OLEarius, Traditional Cache (1/1.5)</desc>
    <url>http://www.geocaching.com/seek/cache_details.aspx?guid=4ac2a247-bd30-463a-96d7-3586abfebfcc</url>
    <urlname>Bücherei WF-Nordost</urlname>
    <sym>Geocache</sym>
    <type>Geocache|Traditional Cache</type>
    <groundspeak:cache id="5109967" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">
        <groundspeak:name>Bücherei WF-Nordost</groundspeak:name>
        <groundspeak:placed_by>OLEarius</groundspeak:placed_by>
        <groundspeak:owner id="6606006">OLEarius</groundspeak:owner>
        <groundspeak:type>Traditional Cache</groundspeak:type>
        <groundspeak:container>Other</groundspeak:container>
        [...]
    </groundspeak:cache>
</wpt>

Using this combination I'm able to create some java classes and parse a geocaching GPX file with all fields defined in base XSD. The unmarshalled object has a getAny()-method, which returns List containing only one element. My IDE tells me that the element is of class ElementNSImpl, not Cache. According to some blog posts the any element of GPX class and the Cache class need specific annotations. But the maven plugin already generates those annotations:

GPX class:

@XmlAnyElement(lax = true)
protected List<Object> any;

Cache class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "cache", namespace = "http://www.groundspeak.com/cache/1/0/1")
public class Cache {

The actual content within the ElementNSImpl object contains all the values of my GPX file. How can I create an object of Cache class directly?

0

There are 0 answers