2015-10-16 34 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, ...); 
相关问题