1

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?

  • Maybe in [this](https://stackoverflow.com/questions/9687588/jaxb-how-to-unmarshall-xsany-xml-string-part) way? – m4gic Sep 09 '18 at 18:16
  • @m4gic: Thank you for the link. First two suggested solutions don't apply to me. I don't understand the third one... Using the fourth one I get Caused by: javax.xml.bind.UnmarshalException: unexpected element (URI:"http://www.groundspeak.com/cache/1/0", lokal:"cache"). expected elements are <{http://www.groundspeak.com/cache/1/0/1}cache> using both 1/0 and 1/0/1 XSD file – darkspirit510 Sep 10 '18 at 17:40
  • If I were you, I would log all responses (as xml), because since you got it seems the XSDs of your service have been changed: the namespace in the response does not match. You can find the current one here: http://static.groundspeak.com/cache/1/0/1/cache.xsd – m4gic Sep 10 '18 at 18:03
  • @m4gic: I tried that but still get this message. What do you mean by logging xml responses? – darkspirit510 Sep 10 '18 at 19:17
  • Something like [this](https://stackoverflow.com/questions/2524301/log-axis2-client-requests-and-responses). IMHO it will be faster to figure out how to solve it when you see the response XML (and, if there is no other option, you can process it using JAXP) – m4gic Sep 10 '18 at 20:02
  • Btw, there is an [API](https://devhub.io/zh/repos/arcao-geocaching-api) for geocaching it seems. If I were you, I would give it a try. – m4gic Sep 10 '18 at 20:08
  • Try creating your JAXB context as follows: `JAXBContext.newInstance(Gpx.class.getPackage().getName() + ":" +Cache.class.getPackage().getName());`. I.e. using package-based context path instead of individual classes. In this case JAXB should be able to unmarshal `JAXBElement` as one element of `any`. – lexicore Sep 15 '18 at 16:16
  • @lexicore: First part did the trick, second part led to exception. It's working now, thank you! – darkspirit510 Sep 16 '18 at 20:26

0 Answers0