我有一個由第三方供應商提供的XSD文件。我需要解析該XSD文件並生成Java對象。我正在使用JAXB通過maven插件來解析XSD文件。使用JAXB在XSD中使用混合類型數據mashlling
一切都很順利,直到我最近需要使用來自正在解析的XML標記之一的數據。該標記的complexType具有mixed = true由於此類,由JAXB生成的java類如下所示。
XSD複雜類型:
<xs:complexType name="Object_Data">
<xs:sequence>
<xs:element name="GeneralRemarks" type="GeneralRemarks" minOccurs="0" maxOccurs="1">
<xs:annotation>
<xs:documentation>General remarks</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="GeneralRemarks" mixed="true">
<xs:sequence>
<xs:element name="GeneralRemark" type ="GeneralRemark" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="GeneralRemark" mixed="true">
<xs:sequence>
<xs:element name="GeneralRemark_RemarkQualifier" type="GeneralRemark_RemarkQualifier" minOccurs="1" maxOccurs="1"/>
<xs:element name="GeneralRemark_RemarkText" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
JAXB類生成
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for GeneralRemarks complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="GeneralRemarks">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="GeneralRemark" type="{}GeneralRemark" maxOccurs="unbounded"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "GeneralRemarks", propOrder = {
"content"
})
public class GeneralRemarks {
@XmlElementRef(name = "GeneralRemark", type = JAXBElement.class)
@XmlMixed
protected List<Serializable> content;
/**
* Gets the value of the content property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the content property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getContent().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
* {@link JAXBElement }{@code <}{@link GeneralRemark }{@code >}
*
*
*/
public List<Serializable> getContent() {
if (content == null) {
content = new ArrayList<Serializable>();
}
return this.content;
}
}
取代具有列表<GeneralRemark>的,GeneralRemarks類包含列表<序列化>。
我已經搜索了很多如何從列表中獲取數據<可序列化的>,並找到了使用下面的代碼來提取數據的解決方案。
GeneralRemarks remarks = xmlObject.getGeneralRemarks();
for(int i=0;i<remarks.getContent().size();i++){
if(remarks.getContent().get(i) instanceof JAXBElement<?>){
JAXBElement j = (JAXBElement)remarks.getContent().get(i);
org.w3c.dom.Element el = (org.w3c.dom.Element) j.getValue();
System.out.println("TEXT--"+el.getTextContent());
System.out.println("TAG--"+el.getTagName());
System.out.println("CHILD --"+el.getElementsByTagName("GeneralRemark_RemarkQualifier").item(0).getTextContent());
}
}
我想知道是否有可能以某種方式覆蓋在智利XSD中的ComplexType或綁定文件(.xjc)要麼重新定義的ComplexType或以某種方式改變混合屬性值設置爲false,以便正確類產生。
我試過在我的xsd中使用Extend/Restrict。還試圖創建一個自定義類型並擴展具有GeneralRemarks類型元素的parentXML節點,以便在子xsd中使用我自定義的複雜類型,但所有這些都不起作用。
我試過Google搜索了很多,但沒有找到任何與此查詢相關的解決方案。大多數鏈接建議只使用擴展/限制,但它們不起作用
請建議是否有任何解決方案以某種方式覆蓋複雜類型。