gpt4 book ai didi

java - 使用@XmlPath 和 jaxb/MOXy 来映射复杂类型

转载 作者:数据小太阳 更新时间:2023-10-29 02:51:32 27 4
gpt4 key购买 nike

我有一个深层的 XML 结构,其中有很多无意义的包装器,我正在映射到一个 Java 类。使用@XmlPath 映射简单数据类型就像在公园里散步一样简单,但是当涉及到实际需要自己的类的类型时,我不太确定该怎么做,尤其是当这些类型也应该放在列表中时。

我在将以下示例中的所有 element 类型映射到我的 Element 类时遇到问题。由于 elements 包装器驻留在使用 @XmlPath 映射的资源中,我不能使用 @XmlElementWrapper,否则我通常会这样会这样做。

示例 XML 结构

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<s:root xsi:schemaLocation="http://www.example.eu/test ResourceSchema.xsd" xmlns:s="http://www.example.eu/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<s:resource>
<s:information>
<s:date>2013-07-04</s:date>
<s:name>This example does not work</s:name>
</s:information>
<s:elements>
<s:refobj>
<s:id>1</s:id>
<s:source>First Source</s:source>
</s:refobj>
<s:refobj>
<s:id>2</s:id>
<s:source>Second Source</s:source>
</s:refobj>
<s:refobj>
<s:id>5</s:id>
<s:source>Fifth Source</s:source>
</s:refobj>
</s:elements>
</s:resource>
</s:root>

根.java

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

@XmlPath("resource/information/date/text()")
private String date;

@XmlPath("s:resource/s:information/s:name/text()")
private String name;

@XmlPath("resource/elements/refobj")
private List<RefObj> refObjs;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getName() {
return name;
}

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

}

RefObj.java

@XmlRootElement(name = "refobj")
@XmlAccessorType(XmlAccessType.FIELD)
public class RefObj {

@XmlElement(name = "id")
private int id;

@XmlElement(name = "source")
private String source;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

}

编码器/解码器

public static void main(String[] args) {
String xml = getXML();

Root root = null;
try {
JAXBContext context = JAXBContext.newInstance(Root.class);

Unmarshaller unmarshaller = context.createUnmarshaller();

StringReader stringReader = new StringReader(xml);

root = (Root) unmarshaller.unmarshal(stringReader);
} catch (Exception ex) {
System.err.println("Failed to unmarshal XML!");
}

try {
JAXBContext context = JAXBContext.newInstance(Root.class);
Marshaller marshaller = context.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.eu/test ResourceSchema.xsd");

StringWriter stringWriter = new StringWriter();
marshaller.marshal(root, stringWriter);

System.out.println(new String(stringWriter.toString().getBytes(Charset.forName("UTF-8"))));
} catch (Exception ex) {
System.err.println("Failed to marshal object!");
}

}

package-info.java

@XmlSchema(
namespace = "http://www.example.eu/test",
attributeFormDefault = XmlNsForm.QUALIFIED,
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns = {
@XmlNs(
prefix = "s",
namespaceURI = "http://www.example.eu/test")
},
location = "http://www.example.eu/test ResourceSchema.xsd")
package se.example.mavenproject1;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

我可以执行该应用程序,但是当我解码/编码 XML 内容时没有映射任何元素,而是我得到一个仅包含信息的 XML 表示。

更新

在发布前面的示例后,我意识到它实际上按预期工作,这让我更加困惑。虽然我试图在我的生产代码中复制(以前的)工作示例但没有成功,尽管我已经设法将我遇到的问题实际引入示例代码中。因为我需要为出现的问题添加命名空间,所以我假设它与命名约定和 X(ml)Path 有关。

我还添加了 package-info.java 和我在处理这些对象时使用的编码器/解码器。由于 jaxb.properties 不包含任何令人兴奋的内容,因此我将其排除在外。

最佳答案

当我运行您的示例时,一切正常。由于您的真实模型可能具有获取/设置方法,因此您需要确保添加 @XmlAccessorType(XmlAccessType.FIELD)到您的类,否则 MOXy(或任何其他 JAXB impl)也会将相应的属性视为已映射(参见:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html)。

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

@XmlPath("resource/information/date/text()")
private String date;

@XmlPath("resource/information/name/text()")
private String name;

@XmlPath("resource/elements/element")
private List<Element> elements;

}

您还需要确保您拥有 jaxb.properties使用以下条目将 MOXy 指定为您的 JAXB 提供程序(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

了解更多信息


更新#1

当您的文档符合 @XmlPath 的命名空间时注释需要考虑到这一点。路径中的节点可以根据 @XmlSchema 进行限定。注解。

包信息

在你的package-info类前缀 s分配给命名空间 URI http://www.example.eu/test .

@XmlSchema(
namespace = "http://www.example.eu/test",
attributeFormDefault = XmlNsForm.QUALIFIED,
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns = {
@XmlNs(
prefix = "s",
namespaceURI = "http://www.example.eu/test")
},
location = "http://www.example.eu/test ResourceSchema.xsd")
package se.example.mavenproject1;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

这意味着符合 http://www.example.eu/test 的节点命名空间应具有前缀 s@XmlPath注释。

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

@XmlPath("s:resource/s:information/s:date/text()")
private String date;

@XmlPath("s:resource/s:information/s:name/text()")
private String name;

@XmlPath("s:resource/s:elements/s:element")
private List<Element> elements;

}

更新#2

So it seems as if the namespace needs to be specified in the @XmlPath when mapping the path to a complex object, but can be skipped when mapping the path to a simple object such as a String or an integer.

这是一个错误。您应该命名空间限定 @XmlPath对于简单的对象,就像你对复杂对象所做的一样(见更新#1)。正确的映射今天有效,我们将修复以下错误,以便不正确的映射能够正常运行。您可以使用下面的链接来跟踪我们在该问题上的进展:

关于java - 使用@XmlPath 和 jaxb/MOXy 来映射复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17466534/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com