2015-10-16 158 views
0

我有一個非常簡單的類,我想以XML形式傳輸。接受的XML格式是由name屬性和字符串值序列組成的字段列表。我可以標註我的課讓我能:如何將簡單Java類的字段名映射爲名稱/值對序列

  1. 使用類名作爲XML實體的屬性「類型」
  2. 使用類的字段的XML序列字段的名稱作爲屬性「名」?

什麼是推薦的方式?這個例子類問題:

class MyEntityType { 
    public List<String> myField1; // = {"A", "B"} 
    public List<String> myField2; // = {"C", "D"} 
} 

以XML格式:

<Entity type="MyEntityType"> 
    <Fields> 
     <Field name="myField1"> 
      <Value>A</Value> 
      <Value>B</Value 
     </Field> 
     <Field name="myField2"> 
      <Value>C</Value> 
      <Value>D</Value> 
     </Field> 
    </Fields> 
</Entity> 

我想什麼來避免被其聲明一個Field類,並把他們的名單在我的對象,因爲我想要執行一組特定的必填字段。

class MyEntityType { 
    public static class Field { 
     String name; 
     String values 
    } 
    public List<Fields> fields; 
} 

出於完整性這些都是支持的架構:

<xs:complexType name="EntityComplexType" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:sequence> 
    <xs:element name="Fields" type="FieldsComplexType" /> 
    </xs:sequence> 
    <xs:attribute name="Type" type="xs:string" use="required" /> 
</xs:complexType> 

<xs:complexType name="FieldsComplexType" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:sequence> 
    <xs:element name="Field" type="FieldComplexType" maxOccurs="unbounded" /> 
    </xs:sequence> 
</xs:complexType> 

<xs:complexType name="FieldComplexType" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:sequence> 
    <xs:element name="Value" minOccurs="0" maxOccurs="unbounded"> 
     <xs:complexType> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"> 
      <xs:attribute name="Alias" type="xs:string" use="optional" /> 
      <xs:attribute name="ReferenceValue" type="xs:string" use="optional" /> 
      </xs:extension> 
     </xs:simpleContent> 
     </xs:complexType> 
    </xs:element> 
    </xs:sequence> 
    <xs:attribute name="Name" use="required" /> 
</xs:complexType> 
+0

您的XML Schema文件與XML示例不匹配。 – laune

回答

1

我想你知道你想要的是從XML標準過程的顯著偏差,忽略此數據格式的設計的基礎概念,以及用於XML綁定的Java體系結構。因此,類字段意味着由元素名稱表示。此外,使用不反映數據結構(類MyEntityType)的XML結構,反之亦然,使用與數據結構不對應的類會產生一個缺陷,即沒有開箱即用的工具可以處理。既沒有註釋也沒有外部綁定來實現這個完全不同的結構。

當然,適配器的JAXB功能有助於將另一種Java類型替換爲另一種Java類型,但這裏必須在將整個Java結構編組爲XML之前替換整個Java結構。

您的XML架構(帶有小的更正)會生成所需的Java類EntityComplexType,FieldsComplexType和FieldComplexType(我不會在此處粘貼)。

Java類EntityAdapter可以寫在適配器的精神:

public class EntityAdapter { 
    public MyEntityType unmarshal(EntityComplexType v){ 
     // ... 
     return new MyEntityType(); 
    } 
    public EntityComplexType marshal(MyEntityType v){ 
     EntityComplexType ect = new EntityComplexType(); 
     ect.setType(v.getClass().getSimpleName()); 
     FieldsComplexType fct = new FieldsComplexType(); 
     ect.setFields(fct); 
     FieldComplexType field1 = new FieldComplexType(); 
     fct.getField().add(field1); 
     field1.setName("myField1"); 
     for(String s: v.getMyField1()){ 
      field1.getValue().add(s); 
     } 
     FieldComplexType field2 = new FieldComplexType(); 
     fct.getField().add(field2); 
     field2.setName("myField2"); 
     for(String s: v.getMyField2()){ 
      field2.getValue().add(s); 
     } 
     return ect; 
    } 
} 

和編組照常進行,通過加入轉化MyEntityType對象到EntityComplexType對象的:

MyEntityType met = ...; 
EntityAdapter adapter = new EntityAdapter(); 
EntityComplexType ect = adapter.marshal(met); 
ObjectFactory of = new ObjectFactory(); 
JAXBElement<EntityComplexType> jbe = of.createEntityComplexType(ect); 
JAXBContext jc = JAXBContext.newInstance(PACKAGE); 
Marshaller m = jc.createMarshaller(); 
m.marshal(jbe, ...); 
相關問題