我有一个非常简单的类,我想以XML形式传输。接受的XML格式是由name属性和字符串值序列组成的字段列表。我可以标注我的课让我能:如何将简单Java类的字段名映射为名称/值对序列
- 使用类名作为XML实体的属性“类型”
- 使用类的字段的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>
您的XML Schema文件与XML示例不匹配。 – laune