Jaxb 注释 - 从 xml 元素中提取 xml 值
1.) 我有一个 XSD 文件(我无法控制),我使用 JAXB 将其转换为对象模型
2.) 我有 XML 格式的数据库提取。 XML 元素标记名称严格是表
3 的字段名称。) 我使用注释将 xml 元素映射到 Java 类。
问题:有没有办法维护 XSD 文件中的元素名称,并仅提取 xml 元素的值。
JAXB 带注释的类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Item", propOrder = {
"code",
"name",
"price"
})
@XmlRootElement(name="inventory")
public class Item {
@XmlElement(name="catalog_num", required = true)
protected String code;
@XmlElement(name="catalog_descrip", required = true)
protected String name;
@XmlElement(name="prod_price")
protected double price;
public String getCode() {
return code;
}
//etc
数据库 xml 文件的摘录:
<?xml version="1.0"?>
<inventory>
<catalog_num>I001</catalog_num>
<catalog_descrip>Descriptive Name of Product</catalog_descrip>
<prod_price>11200</prod_price>
</inventory>
编组上述 xml 文件后需要得到的结果是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Item>
<code>I001</code>
<name>Descriptive Name of Product</name>
<price>11200.0</price>
</Item>
在上面的代码中,我尝试注释方法而不是字段,但产生相同的结果。我只想从 xml 元素中提取值,但不更改元素名称。
我希望我说得有道理。
1.) I have an XSD file (I have no control over) that I converted to object model using JAXB
2.) I have a database extract in XML format. The XML element tag names are strictly the field names of the table
3.) I mapped the xml elements to the Java class using annotations.
Question: Is there a way to maintain the element names in the XSD file, and JUST extract the value of the xml elements.
JAXB annotated class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Item", propOrder = {
"code",
"name",
"price"
})
@XmlRootElement(name="inventory")
public class Item {
@XmlElement(name="catalog_num", required = true)
protected String code;
@XmlElement(name="catalog_descrip", required = true)
protected String name;
@XmlElement(name="prod_price")
protected double price;
public String getCode() {
return code;
}
//etc
An excerpt of the database xml file:
<?xml version="1.0"?>
<inventory>
<catalog_num>I001</catalog_num>
<catalog_descrip>Descriptive Name of Product</catalog_descrip>
<prod_price>11200</prod_price>
</inventory>
The result I need to get after marshaling the above xml file is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Item>
<code>I001</code>
<name>Descriptive Name of Product</name>
<price>11200.0</price>
</Item>
In the above code, I have tried annotating the methods instead of the fields, but I yield the same result. I just want the value extracted from the xml elements, but not change the element names.
I hope I am making sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:我是 EclipseLink JAXB (MOXy) 负责人,以及 JAXB (JSR-222) 专家组成员。
MOXy 提供了一个扩展,您可以通过 XML 文档应用第二个 XML 绑定。此绑定文档可用于添加元数据,或者当
xml-mapping-metadata-complete="true"
完全替换 Java 模型上的 JAXB 注释提供的元数据时;创建 JAXBContext 时,绑定文件作为参数传递:
要解决您的问题,您可以创建一个 JAXBContext 来处理数据库文档(使用带注释的类),并创建第二个 JAXBContext 来使用 MOXy 绑定文件处理结果格式。下面是它的外观:
有关更详细的示例,请参见:
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.
MOXy offers an extension where you can apply a second XML binding via an XML document. This binding document can either be used to add metadata, or when
xml-mapping-metadata-complete="true"
completely replace the metadata supplied by the JAXB annotations on the Java model;The bindings file is passed as a parameter when creating the JAXBContext:
To solve your issue you could create one JAXBContext to handle the database document (using the annotated classes), and create a second JAXBContext to handle the result format using the MOXy binding file. Below is how this would look:
For a More Detailed Example See:
如果我理解正确的话,您正在寻求以两种不同的方式编组和解组对象。虽然不完全是它的预期用途,但您也许可以使用 XmlJavaTypeAdapter 为此。这里有一个教程。
If I understand correctly, you're looking to marshal and unmarshal an object in two different ways. While not quite the situation it's intended for, you might be able to use an XmlJavaTypeAdapter for that. There's a tutorial here.