2015-10-13 130 views
1

我有一個相當複雜的XML樹結構,其中包含很多元素。對於每個元素,提交者可以選擇該值是否保密。目前,我認爲解決的辦法是像下面的例子:具有屬性和內容的元素的XSD模式和JAXB類

<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">[email protected]<Email> 
    <Phone confidential="true">+33110111213<Phone> 
    ... 
</Person> 

我不是專家,但我想,以避免生成特定類型(XSD架構)和特定的類(使用JAXB)爲每個元素。可能嗎 ?否則,你有什麼想法來解決我的問題嗎?

非常感謝您的幫助

回答

0

您的問題有點不清楚。什麼是你想避免 我的方法你想要做的是這樣的事情

  1. 形成正確的XML的所有元素你想做的定義 您可以驗證在這裏你的XML Validate an 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> 
     
        </Addresses> 
     
        <Email confidential="true">[email protected]</Email> 
     
        <Phone confidential="true">+33110111213</Phone> 
     
    </Person>

  2. 生成你從這裏XML to XSD generator有XML XML模式有3種算法/模式

  3. 在eclipse中生成JAXB類。 Eclipse有輸入功能,它允許您生成JAXB類Generating JAXB Classes from a Schema

0

很抱歉,如果我不明白。事實上,我已經生成了我的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。這是我想避免的,但也許這是不可能的? 否則,爲了管理數據的機密性,我對任何其他命題都表示同意?

感謝

+0

你應該爲編輯這些附加信息添加到你的問題,而不是作爲一個答案。 – RCB

0

你可以在你的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是這樣的(你只需要爲你想成爲機密的人提供的屬性,因爲默認的是假):

<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; 
     } 
    } 
} 
+0

我沒有想過這個解決辦法,但我喜歡它。非常感謝您 – sylmandel

+0

@sylmandel沒問題。如果您覺得有用,請考慮投票並接受我的答案。 – RCB

相關問題