2012-09-24 2 views
6

Ich habe diese XML-Struktur:Wie behandelt man JAXB ComplexType mit MixedContent-Daten?

<Tax> 
    <Money currency="USD">0.00</Money> 
    <Description xml:lang="en"> 
    17.5% Non-Recoverable 
    <ShortName>vatspecial</ShortName> 
    </Description> 
</Tax> 

Beachten Sie, dass Description Knoten MixedContent(zusammengesetzt mit Text und XML) und dies ist der XSD Teil in Bezug auf Description Knoten hat:

<xsd:complexType name="TaxDescriptionType"> 
    <xsd:sequence> 
    <xsd:element name="ShortName" type="xsd:string" /> 
    </xsd:sequence> 
    <xsd:attribute ref="xml:lang" /> 
</xsd:complexType> 

Alles ist in Ordnung, XJC gibt das Generat aus ed Klassen wie diese in Bezug auf TaxDescriptionType:

package org.com.project; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlSchemaType; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

/** 
* <p>Java class for TaxDescriptionType complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType name="TaxDescriptionType"> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="ShortName" type="{http://www.w3.org/2001/XMLSchema}string"/> 
*  &lt;/sequence> 
*  &lt;attribute ref="{http://www.w3.org/XML/1998/namespace}lang"/> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "TaxDescriptionType", propOrder = { 
    "shortName" 
}) 
public class TaxDescriptionType { 

    @XmlElement(name = "ShortName", required = true) 
    protected String shortName; 
    @XmlAttribute(name = "lang", namespace = "http://www.w3.org/XML/1998/namespace") 
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 
    @XmlSchemaType(name = "NCName") 
    protected String lang; 

    /** 
    * Gets the value of the shortName property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getShortName() { 
     return shortName; 
    } 

    /** 
    * Sets the value of the shortName property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setShortName(String value) { 
     this.shortName = value; 
    } 

    /** 
    * Gets the value of the lang property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getLang() { 
     return lang; 
    } 

    /** 
    * Sets the value of the lang property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setLang(String value) { 
     this.lang = value; 
    } 

} 

Dann mit dem oben class ich in der Lage bin mit den Elementen so arbeiten um:

taxDescriptionType.setLang("en"); 
taxDescriptionType.setShortName("vatspecial"); 
/* missing value: 17.5% Non-Recoverable */ 

Aber das Problem ist, dass ich Einen Weg zu get oder set17.5% Non-Recoverable Text der aus dem obigen XML Beispiel kann nicht gefunden werden.


Das ist, was ich versucht und es funktioniert nicht:

  • Gebrauchte mixed="true" Attribut wie folgt aus:

<xsd:complexType name="TaxDescriptionType" mixed="true">

(Ich denke, XJC ist Ignorieren der l ast Attribut)


tun, einige der Forschung, fand ich dies:

JAXB XJC compiler disregarding mixed=true on XML Schema documents

Aber ich bin nicht sicher, ob dies der richtige Weg ist, diese zu lösen. Eine der Antworten gesagt, dass dies ein Fehler ist und in dem anderen zeigt einen Code, der die MixedContent in einen List<Serializable> und vielleicht die nächste Situation dies wird über den Umgang verwandelt:

taxDescriptionType.getContent().add(Serializable element); 

(Und ich weiß wirklich nicht, wie man mit einem Serializable Elemente beschäftigen)

Antwort

4

wie Sie erwähnen Sie die mixed Attribute hinzufügen müssen, um anzuzeigen, dass Ihre Art gemischte Inhalte unterstützt. Ohne dies Ihr XML angegebenen Inhalt ist ungültig:

<xsd:complexType name="TaxDescriptionType" mixed="true"> 
    <xsd:sequence> 
     <xsd:element name="ShortName" type="xsd:string" /> 
    </xsd:sequence> 
    <xsd:attribute ref="xml:lang" /> 
</xsd:complexType> 

Die erzeugte TaxDescriptionType Klasse wird die folgende Eigenschaft haben.Im Wesentlichen bedeutet dies, dass der gesamte nicht attributive Inhalt in einer List gespeichert wird. Dies ist erforderlich, da Sie einen Mechanismus benötigen, der angibt, wo sich die Textknoten im Elementinhalt befinden.

@XmlElementRef(name = "ShortName", namespace = "http://www.example.org/schema", type = JAXBElement.class) 
@XmlMixed 
protected List<Serializable> content; 

Sie diese Liste mit Instanzen von String (die Textknoten) bevölkern und JAXBElement (Elementgehalt darstellt).


HILFS

Mixed Inhalt der Regel macht das Leben komplizierter, als es sein muss. Wenn möglich, würde ich eine alternative XML-Darstellung empfehlen.

<Tax> 
    <Money currency="USD">0.00</Money> 
    <Description xml:lang="en" ShortName="vatspecial"> 
    17.5% Non-Recoverable 
    </Description> 
</Tax> 

Oder

<Tax> 
    <Money currency="USD">0.00</Money> 
    <Description xml:lang="en"> 
    <LongName>17.5% Non-Recoverable</LongName> 
    <ShortName>vatspecial</ShortName> 
    </Description> 
</Tax> 
+0

Ist der Parameter 'Serializable' von' List' signifikant? –

1

Mit gemischten = true, in Object sollte es eine Funktion wie JAXBElement<ShortNameType> createTaxDescriptionTypeShortNameType(ShortNameType) sein, die für Sie das serializable Element erzeugt.

@XmlElementDecl(namespace = "", name = "shortnametype", scope = TaxDescriptionType.class) 
    public JAXBElement<ShortNameType> createTaxDescriptionTypeShortNameType(ShortNameType value) { 
     return new JAXBElement<ShortNameType>(new QName("", "shortnametype"), ShortNameType.class, TaxDescriptionType.class, value); 
} 
Verwandte Themen