我在谷歌上能找到的只有 3 条信息,最详细的是设计文档,如下 URL
http://wiki.eclipse.org/EclipseLink/DesignDocs/317962/Phase2.1#.40XmlProperty
它确实提到“xml-property 元数据标记将用于配置属性”。并且给出了两个没有编组结果的示例如下:
Example: type-level property
The following example will demonstrate how a type-level property can be applied.
Setting xml-property on a type via EclipseLink XML metadata can be accomplished as follows:
<java-type name="org.example.Employee">
    <xml-properties>
        <xml-property name="identifier" value="101" value-type="java.lang.Integer" />
        <xml-property name="isTrue" value="false" value-type="java.lang.Boolean" />
    </xml-properties>
</java-type>
Setting @XmlProperty on a type via annotations can be accomplished as follows:
org.example.Employee.java
@XmlProperties({@XmlProperty(name="identifier", value="101", valueType=Integer.class), 
                @XmlProperty(name="isTrue", value="false", valueType=Boolean.class)})
public class Employee {
   ...
}
Example: property-level property
The following example will demonstrate how a property-level property can be applied.
Setting xml-property on a property via EclipseLink XML metadata can be accomplished as follows:
<java-type name="org.example.Employee">
    <java-attributes>
        <xml-element java-attribute="myelement">
            <xml-properties>
                <xml-property name="isAttribute" value="false" value-type="java.lang.Boolean" />
                <xml-property name="comment" value="this is an element" />
            </xml-properties>
        </xml-element>
    </java-attributes>
</java-type>
Setting @XmlProperty on a property via annotations can be accomplished as follows:
org.example.Employee.java
public class Employee {
  @XmlProperties({@XmlProperty(name="isAttribute", value="false", valueType=Boolean.class),
                  @XmlProperty(name="comment", value="this is an element")}
  public String myelement;
}
我也尝试了我的示例,但我无法区分使用和不使用 xml-property 的东西。
谁能向我解释 XmlProperty 是做什么的?它会产生什么样的效果?或何时使用 XmlProperty?是否有任何带有封送结果的示例代码?
<?xml version="1.0"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd"
    version="2.1">
    <xml-schema element-form-default="QUALIFIED">
        <xml-ns prefix="ns1" namespace-uri="http://www.example.org/customer" />
        <xml-ns prefix="ns2" namespace-uri="http://www.example.org/phone" />
        <xml-ns prefix="ns3" namespace-uri="http://www.example.org/addr" />
    </xml-schema>
    <java-types>
        <java-type name="example.gettingstarted.demo1.Customer">
            <xml-root-element />
            <xml-type namespace="http://www.example.org/customer"
                prop-order="name address phoneNumbers" />
            <xml-properties>
                <xml-property name="hello" value="false" value-type="java.lang.String" />
                <xml-property name="world" value="this is an element"
                    value-type="java.lang.String" />
            </xml-properties>
            <java-attributes>
                <xml-attribute java-attribute="name" xml-path="@name" />
                <xml-element java-attribute="address">
                </xml-element>
                <xml-element java-attribute="phoneNumbers" xml-path="contact-info/phone-number" />
            </java-attributes>
        </java-type>
        <java-type name="example.gettingstarted.demo1.PhoneNumber">
            <xml-root-element />
            <xml-type namespace="http://www.example.org/phone"></xml-type>
            <java-attributes>
                <xml-attribute java-attribute="type" />
                <xml-value java-attribute="value" />
            </java-attributes>
        </java-type>
        <java-type name="example.gettingstarted.demo1.Address">
        </java-type>
    </java-types>
</xml-bindings>
Java 文件:
package example.gettingstarted.demo5;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import example.gettingstarted.demo1.Address;
import example.gettingstarted.demo1.Customer;
import example.gettingstarted.demo1.PhoneNumber;
/**
 * @author barry
 * make Customer.Address.street as Customer.@street
 */
public class Demo {
    @SuppressWarnings({ "rawtypes", "deprecation" })
    public static void main(String[] args) throws JAXBException {
        // Step 1 - Create the Domain Model
        Customer customer = new Customer();
        customer.setName("Jane Doe");
        Address address = new Address();
        address.setStreet("123 Any Street");
        address.setCity("My Town");
        customer.setAddress(address);
        PhoneNumber workPhoneNumber = new PhoneNumber();
        workPhoneNumber.setType("work");
        workPhoneNumber.setValue("613-555-1111");
        customer.getPhoneNumbers().add(workPhoneNumber);
        PhoneNumber cellPhoneNumber = new PhoneNumber();
        cellPhoneNumber.setType("cell");
        cellPhoneNumber.setValue("613-555-2222");
        customer.getPhoneNumbers().add(cellPhoneNumber);
        // Step 2 - Convert the Domain Model to XML
        final Map<String, Source> metadataSourceMap = new HashMap<String, Source>();
        metadataSourceMap.put("example.gettingstarted.demo1", new StreamSource("./example/gettingstarted/demo5/eclipselink-oxm.xml"));
        final Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadataSourceMap);
        final Class[] classes = new Class[1];
        classes[0] = Customer.class;
        JAXBContext jaxbContext = (JAXBContext) JAXBContext.newInstance(classes, properties);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }
}
无论是否应用了 xml-property,我的输出如下。
My Town 123 Any Street 613-555-1111 613-555-2222