sylmandel
sylmandel

Reputation: 51

XSD schema and JAXB classes for element with attribute and content

I have a quite complex XML tree structure which contains a lot of elements. For each element, the submitter can choose if the value is confidential or not. Currently, the solution that I consider is something like the following example:

<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>

I am not a specialist but I would like to avoid generating a specific type (in the XSD schema) and a specific class (using JAXB) for each element. Is it possible ? Otherwise, do you have any idea in order to solve my problem ?

Many thanks in advance for your help

Upvotes: 2

Views: 2484

Answers (4)

dennis_chen_canada
dennis_chen_canada

Reputation: 82

If I understand correctly, maybe this is related to your issue. I am using 2 methods to generate java objects from XML/XSD.

  1. eclipse tool (step-by-step detail with screen capture here : https://sites.google.com/site/canadadennischen888/home/xml/jaxb-xml-to-pojo/xml-to-pojo/eclipse-feature )

  2. xjc ( xjc file_name.xsd -d folder_name -p package )

hope it help

Upvotes: 0

RCB
RCB

Reputation: 845

You could do this in your 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>

So your XML would look like this (you only have to supply attributes for the ones you want to be confidential because the default is false):

<Person lastNameConfidential="true">
    <LastName>Doe</LastName>
    <FirstName>John</FirstName>        
</Person>

And the generated JAXB class would look like:

@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;
        }
    }
}

Upvotes: 2

sylmandel
sylmandel

Reputation: 51

Sorry if I am not clear. In fact, I have already generate my XSD and the corresponding Java classes. My problem is that I would like to avoid, if it is possible, generating something like that:

XSD type:

<xs:complexType name="ZipCodeType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="confidential"/>
      </xs:extension>
    </xs:simpleContent>
</xs:complexType>

and its corresponding Java classes:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ZipCodeType", propOrder = {
    "value"
})
public class ZipCodeType {

@XmlValue
protected String value;
@XmlAttribute(name = "confidential")
protected String confidential;

I have a big XML structure and using this method I will have one type/class per element so it means many classes and a very big XSD. This is what I would like to avoid but perhaps it is not possible ? Otherwise I am interessting for any other proposition in order to manage the confidentiality of the data ?

Thanks

Upvotes: 0

Acewin
Acewin

Reputation: 1727

Your question is a bit unclear. What are you trying to avoid My approach to what you want to do would be something like this

  1. Form correct XML with all the elements you wanna define You can validate your XML here 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. Generate XML schema for the XML you have from here XML to XSD generator There are 3 algorithms/patterns

  3. Generate JAXB classes in eclipse. Eclipse has input functionality which allows you to generate JAXB classes Generating JAXB Classes from a Schema

Upvotes: 1

Related Questions