2013-03-27 15 views
1

我可以找到關於谷歌只有3個資料片,以及最詳細的設計文檔如下網址我無法理解EclipseLink MOXy XmlProperty或xml屬性的含義,任何人都可以提供更多信息嗎?

http://wiki.eclipse.org/EclipseLink/DesignDocs/317962/Phase2.1#.40XmlProperty

它提到:「該XML屬性的元數據標籤將用於配置一個屬性「。並沒有編組結果兩個例子給出如下:

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屬性的東西之間的任何差異。

任何人都可以向我解釋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 [email protected] 
*/ 
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屬性。

我鎮 123條街 613-555-1111 613-555-2222

回答

0

EclipseLink JAXB (MOXy)@XmlProperty擴展是一種方式來獲得自定義數據到莫西的本地映射元數據。這是一些非常獨特的用例。我將演示如何在下面使用它。

Java模型

我會用這個例子以下模型。

package forum15651379; 

public class Foo { 

    private String bar; 

    public String getBar() { 
     return bar; 
    } 

    public void setBar(String bar) { 
     this.bar = bar; 
    } 

} 

DEMO CODE

JAXBContext下面的演示代碼將在MOXY的外部映射文件自舉(參見:http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html)。

package forum15651379; 

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, "forum15651379/oxm.xml"); 
     JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties); 

     Foo foo = new Foo(); 
     foo.setBar("Hello World"); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(foo, System.out); 
    } 

} 

常見的情況

下面是典型的用例。映射由java-attributes元素定義。

oxm。XML

<?xml version="1.0" encoding="UTF-8"?> 
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    package-name="forum15651379"> 
    <java-types> 
     <java-type name="Foo"> 
      <xml-root-element name="FOO"/> 
      <java-attributes> 
       <xml-element java-attribute="bar" name="BAR"> 
       </xml-element> 
      </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

輸出

<?xml version="1.0" encoding="UTF-8"?> 
<FOO> 
    <BAR>Hello World</BAR> 
</FOO> 

高級用例

oxm.xml

我都擴大了oxm.xml,包括性能,並有SP描述了一個描述符定製器。

<?xml version="1.0" encoding="UTF-8"?> 
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    package-name="forum15651379"> 
    <java-types> 
     <java-type name="Foo" xml-customizer="forum15651379.FooCustomizer"> 
      <xml-properties> 
       <xml-property name="key1" value="value1"/> 
       <xml-property name="key2" value="value2"/> 
      </xml-properties> 
      <xml-root-element name="FOO"/> 
      <java-attributes> 
       <xml-element java-attribute="bar" name="BAR"> 
        <xml-properties> 
         <xml-property name="key3" value="value3"/> 
         <xml-property name="key4" value="value4"/> 
        </xml-properties> 
       </xml-element> 
      </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

描述定製(FooCustomizer)

描述符定製,您可以訪問莫西的本地映射元數據。以下是您可以使用oxm.xml文件中指定的屬性做什麼的簡單示例。

package forum15651379; 

import org.eclipse.persistence.config.DescriptorCustomizer; 
import org.eclipse.persistence.descriptors.ClassDescriptor; 
import org.eclipse.persistence.oxm.XMLDescriptor; 
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping; 

public class FooCustomizer implements DescriptorCustomizer{ 

    @Override 
    public void customize(ClassDescriptor descriptor) throws Exception { 
     XMLDescriptor xmlDescriptor = (XMLDescriptor) descriptor; 
     String key1Value = (String) xmlDescriptor.getProperty("key1"); 
     xmlDescriptor.setDefaultRootElement(key1Value); 

     XMLDirectMapping barMapping = (XMLDirectMapping) xmlDescriptor.getMappingForAttributeName("bar"); 
     String key3Value = (String) barMapping.getProperty("key3"); 
     barMapping.setXPath(key3Value + "/text()"); 
    } 

} 

輸出

下面我們看到的是我們在FooCustomizer做了生成的XML的影響。

<?xml version="1.0" encoding="UTF-8"?> 
<value1> 
    <value3>Hello World</value3> 
</value1> 
+1

謝謝布萊斯!我非常感謝你的回答和整潔的演示。我非常喜歡MOXY,特別是運行時動態元數據驅動的風格。這將是多租戶平臺內關鍵組件的首選。 – 2013-04-10 12:28:17

+0

@BarryZhong - 謝謝巴里。我們在頭腦中開發了多租戶MOXY。這裏是一個你可能會感興趣的例子的鏈接:http://blog.bdoughan.com/2011/06/moxy-extensible-models-multi-tenant.html – 2013-04-10 12:51:04

+1

是的,這真的很酷。我已經閱讀並嘗試了wiki.eclipse.org中的大部分MOXy示例。我確實嘗試過這個例子,我發現它本質上是多租戶的,這正是我想要的。謝謝布萊斯你爲世界做了什麼,我羨慕你。 – 2013-04-11 06:36:49

相關問題