我有一个非常复杂的XML树结构,其中包含许多元素。对于每个元素,提交者可以选择值是否是机密的。目前,我考虑的解决方案类似于以下示例:
<Person>
<Lastname confidential="true">Doe<Lastname>
<Fistname confidential="false">John<Fistname>
<Addresses>
<Address>
<Street confidential="false">aaaaaaa</Street>
<ZipCode confidential="true">75000</ZipCode>
<City confidential="false">Paris</City>
<Country confidential="true">FR</Country>
</Address>
...
<Adresses>
<Email confidential="true">john.doe@mail.com<Email>
<Phone confidential="true">+33110111213<Phone>
...
</Person>我不是专家,但我希望避免为每个元素生成特定的类型(在XSD模式中)和特定的类(使用JAXB)。有可能吗?否则,你有什么办法来解决我的问题吗?
非常感谢您的帮助
发布于 2015-10-16 05:22:33
您可以在您的xsd中执行此操作:
<xsd:complexType name="Person">
<xsd:sequence>
<xsd:element name="LastName" type="xsd:string"/>
<xsd:element name="FirstName" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="lastNameConfidential" type="xsd:boolean" default="false"/>
<xsd:attribute name="firstNameConfidential" type="xsd:boolean" default="false"/>
</xsd:complexType>因此,您的XML将如下所示(您只需为您希望保密的属性提供属性,因为缺省值为false):
<Person lastNameConfidential="true">
<LastName>Doe</LastName>
<FirstName>John</FirstName>
</Person>生成的JAXB类将如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Person", propOrder = {
"lastName",
"firstName"
})
public class Person {
@XmlElement(name = "LastName", required = true)
protected String lastName;
@XmlElement(name = "FirstName", required = true)
protected String firstName;
@XmlAttribute(name = "lastNameConfidential")
protected Boolean lastNameConfidential;
@XmlAttribute(name = "firstNameConfidential")
protected Boolean firstNameConfidential;
// Code ommitted
public boolean isLastNameConfidential() {
if (lastNameConfidential == null) {
return false;
} else {
return lastNameConfidential;
}
}
public boolean isFirstNameConfidential() {
if (firstNameConfidential == null) {
return false;
} else {
return firstNameConfidential;
}
}
}发布于 2015-10-14 06:27:00
你的问题有点不清楚。对于你想要做的事情,你试图避免我的方法是这样的吗?
无名氏John aaaaaaa 75000巴黎FR john.doe@mail.com +33110111213
这里有3种算法/模式,
发布于 2015-10-14 14:56:59
如果我说得不清楚,很抱歉。实际上,我已经生成了XSD和相应的Java类。我的问题是,如果可能的话,我想避免生成这样的东西:
XSD类型:
<xs:complexType name="ZipCodeType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="confidential"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>及其对应的Java类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ZipCodeType", propOrder = {
"value"
})
public class ZipCodeType {
@XmlValue
protected String value;
@XmlAttribute(name = "confidential")
protected String confidential;我有一个很大的XML结构,使用这个方法,每个元素都有一个类型/类,所以这意味着有很多类和一个非常大的XSD。这是我想要避免的,但也许这是不可能的?否则,我是否对任何其他建议感兴趣,以便管理数据的机密性?
谢谢
https://stackoverflow.com/questions/33112529
复制相似问题