【问题标题】:JaxB Xpath : How to use multiple xpath to a same java bean?JaxB Xpath:如何对同一个 java bean 使用多个 xpath?
【发布时间】:2012-10-03 08:20:23
【问题描述】:

我需要使用 jaxb 生成 xml,如下所示:

<item>
    <key1 id="default">value1</key1>
    <key2 id="default">value2</key2>
    <key3 id="default">value3</key3>
</item>

如何在 jaxb 中使用 @XmlPath?

我用过以下一种。但是我有多个 50 左右的键。如何实现这一点?

   @XmlPath("key1/@id")
    private String attrValue = "default";

【问题讨论】:

    标签: jaxb for-xml-path


    【解决方案1】:

    @XmlPathJAXB (JSR-222)EclipseLink MOXy 实现的扩展。您需要使用 MOXy 映射文件中的等效项来获得所需的行为。

    oxm.xml

    您正在寻找的是为字段/属性应用多个可写映射的能力。这目前无法通过注释来完成,但可以使用 MOXy 的外部映射文档来完成。

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum12704491">
        <java-types>
            <java-type name="Item">
                <java-attributes>
                    <xml-element java-attribute="attrValue" xml-path="key1/@id"/>
                    <xml-element java-attribute="attrValue" xml-path="key2/@id" write-only="true"/>
                    <xml-element java-attribute="attrValue" xml-path="key3/@id" write-only="true"/>
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    物品

    package forum12704491;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Item {
    
        private String attrValue;
        private String key1;
        private String key2;
        private String key3;
    
    }
    

    jaxb.properties

    为了将 MOXy 指定为您的 JAXB 提供程序,您需要在与域模型相同的包中添加一个名为 jaxb.properties 的文件,并使用以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

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

    演示

    下面的演示代码演示了如何使用 MOXy 的外部映射文档进行引导。

    package forum12704491;
    
    import java.io.File;
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum12704491/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Item.class}, properties);
    
            File xml = new File("src/forum12704491/input.xml");
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Item item = (Item) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(item, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <item>
       <key1 id="default">value1</key1>
       <key2 id="default">value2</key2>
       <key3 id="default">value3</key3>
    </item>
    

    【讨论】:

    • 我知道上面的答案。这可能吗? @XmlPath("key1/@id"),@XmlPath("key2/@id"),@XmlPath("key3@id") private String attrValue = "default";
    • 有关此功能是否以及何时可用作注释的任何消息?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    相关资源
    最近更新 更多