Java 让MOXy@XmlPath使用名称空间

Java 让MOXy@XmlPath使用名称空间,java,xml,jaxb,eclipselink,moxy,Java,Xml,Jaxb,Eclipselink,Moxy,我正在编写一个使用JAXB和MOXy解析KML文件的脚本,但是我很难让@XmlPath使用提供的名称空间 如果我的KML如下所示:- <kml> <Document> <name>Test</name> </Document> </kml> @XmlRootElement(name = "kml") public class Kml { @XmlPath("Document/name

我正在编写一个使用JAXB和MOXy解析KML文件的脚本,但是我很难让@XmlPath使用提供的名称空间

如果我的KML如下所示:-

<kml>
    <Document>
        <name>Test</name>
    </Document>
</kml>
@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;
    }
}
<kml xmlns="http://www.opengis.net/kml/2.2">
    <Document>
        <name>Test</name>
    </Document>
</kml>
@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;
    }
}
。。。然后,
kml.getName()

但是,如果我的KML包含如下名称空间:-

<kml>
    <Document>
        <name>Test</name>
    </Document>
</kml>
@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;
    }
}
<kml xmlns="http://www.opengis.net/kml/2.2">
    <Document>
        <name>Test</name>
    </Document>
</kml>
@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;
    }
}
。。。然后,
kml.getName()
返回
null

我在正确的包级别上有
jaxb.properties
,并且我使用了以下MOXy的依赖项:-

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

org.eclipse.persistence
org.eclipse.persistence.moxy
2.3.2

我到底错过了什么?谢谢。

下面的示例演示了如何配置名称空间信息

套餐信息

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);
    }

}
您可以使用
@XmlSchema
注释来指定名称空间信息和限定。在下面的示例中,我们将指定名称空间,默认情况下,所有元素都应该是名称空间限定的

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

import javax.xml.bind.annotation.*;
Kml

我们不需要在
Kml
类中指定任何名称空间信息。此信息来自
软件包信息中的设置:

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;
    }
}
演示

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>

试验
了解更多信息


下面是一个示例,演示如何配置命名空间信息

套餐信息

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);
    }

}
您可以使用
@XmlSchema
注释来指定名称空间信息和限定。在下面的示例中,我们将指定名称空间,默认情况下,所有元素都应该是名称空间限定的

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

import javax.xml.bind.annotation.*;
Kml

我们不需要在
Kml
类中指定任何名称空间信息。此信息来自
软件包信息中的设置:

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;
    }
}
演示

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>

试验
了解更多信息