limc
limc

Reputation: 40160

Getting MOXy @XmlPath to Work with Namespace

I'm writing a script to parse a KML file using JAXB and MOXy, but I'm having difficulty getting @XmlPath to work with a provided namespace.

If my KML looks like this:-

<kml>
    <Document>
        <name>Test</name>
    </Document>
</kml>

... and my bean looks like this:-

@XmlRootElement(name = "kml")
public class Kml {
    @XmlPath("Document/name/text()")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

... then, kml.getName() returns Test, which works like it should.

However, if my KML contains a namespace like this:-

<kml xmlns="http://www.opengis.net/kml/2.2">
    <Document>
        <name>Test</name>
    </Document>
</kml>

... and my bean looks like this:-

@XmlRootElement(name = "kml", namespace = "http://www.opengis.net/kml/2.2")
public class Kml {
    @XmlPath("Document/name/text()")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

... then, kml.getName() returns null.

I do have jaxb.properties at the right package level and I'm using the following MOXy's dependency:-

<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>org.eclipse.persistence.moxy</artifactId>
   <version>2.3.2</version>
</dependency>

What exactly am I missing here? Thanks.

Upvotes: 1

Views: 1620

Answers (1)

bdoughan
bdoughan

Reputation: 148977

Below is an example demonstrating how to configure the namespace information.

package-info

You can use the @XmlSchema annotation to specify the namespace information and qualification. In the example below we will specify the namespace, and that by default all elements should be namespace qualified.

@XmlSchema(
    namespace="http://www.opengis.net/kml/2.2", 
    elementFormDefault=XmlNsForm.QUALIFIED)
@XmlAccessorType(XmlAccessType.FIELD)
package forum9931520;

import javax.xml.bind.annotation.*;

Kml

We do not need to specify any namespace information in the Kml class. This information comes from the settings in package-info:

package forum9931520;

import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "kml")
public class Kml {
    @XmlPath("Document/name/text()")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Demo

package forum9931520;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Kml.class);

        File xml = new File("src/forum9931520/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Kml kml = (Kml) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(kml, System.out);
    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
   <Document>
      <name>Test</name>
   </Document>
</kml>

For More Information

Upvotes: 1

Related Questions