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.
Below is an example demonstrating how to configure the namespace information.
package-info
You can use the
@XmlSchemaannotation 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.Kml
We do not need to specify any namespace information in the
Kmlclass. This information comes from the settings inpackage-info:Demo
input.xml/Output
For More Information