Jaxb Annotations - 从 xml 元素中提取 xml 值 [英] Jaxb Annotations - Extract xml value from xml element

查看:30
本文介绍了Jaxb Annotations - 从 xml 元素中提取 xml 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.) 我有一个 XSD 文件(我无法控制),我使用 JAXB 将其转换为对象模型

1.) I have an XSD file (I have no control over) that I converted to object model using JAXB

2.) 我有一个 XML 格式的数据库摘录.XML 元素标签名称严格为表的字段名称

2.) I have a database extract in XML format. The XML element tag names are strictly the field names of the table

3.) 我使用注释将 xml 元素映射到 Java 类.

3.) I mapped the xml elements to the Java class using annotations.

问题:有没有办法在 XSD 文件中维护元素名称,而只需提取 xml 元素的值.

Question: Is there a way to maintain the element names in the XSD file, and JUST extract the value of the xml elements.

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文件摘录:

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>

对上述xml文件进行编组后我需要得到的结果是:

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>

在上面的代码中,我尝试注释方法而不是字段,但我产生了相同的结果.我只想要从 xml 元素中提取的值,而不是更改元素名称.

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.

我希望我说得有道理.

推荐答案

注意:我是EclipseLink JAXB (MOXy) 负责人,也是 JAXB 的成员 (JSR-222) 专家组.

Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.

MOXy 提供了一个扩展,您可以在其中通过 XML 文档应用第二个 XML 绑定.此绑定文档可用于添加元数据,或者当 xml-mapping-metadata-complete="true" 完全替换 Java 模型上的 JAXB 注释提供的元数据时;

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;

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum6838882" 
    xml-mapping-metadata-complete="true">
    <java-types>
        <java-type name="Item" xml-accessor-type="FIELD">
            <xml-root-element name="Item"/>
        </java-type>
    </java-types>
</xml-bindings>

绑定文件在创建 JAXBContext 时作为参数传递:

The bindings file is passed as a parameter when creating the JAXBContext:

Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum6838882/binding.xml");
JAXBContext resultJC = JAXBContext.newInstance(new Class[] {Item.class}, properties);

要解决您的问题,您可以创建一个 JAXBContext 来处理数据库文档(使用带注释的类),并创建第二个 JAXBContext 来使用 MOXy 绑定文件处理结果格式.下面是它的样子:

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:

package forum6838882;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext databaseJC = JAXBContext.newInstance(Item.class);
        Unmarshaller databaseUnmarshaller = databaseJC.createUnmarshaller();
        File databaseXML = new File("src/forum6838882/database.xml");
        Item item = (Item) databaseUnmarshaller.unmarshal(databaseXML);

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum6838882/binding.xml");
        JAXBContext resultJC = JAXBContext.newInstance(new Class[] {Item.class}, properties);
        Marshaller resultMarshaller = resultJC.createMarshaller();
        resultMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        resultMarshaller.marshal(item, System.out);
    }
}

有关更详细的示例,请参阅:

这篇关于Jaxb Annotations - 从 xml 元素中提取 xml 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆