2016-09-30 88 views
0

我有一个由第三方供应商提供的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> 
* &lt;complexType name="GeneralRemarks"> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;element name="GeneralRemark" type="{}GeneralRemark" maxOccurs="unbounded"/> 
*  &lt;/sequence> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/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搜索了很多,但没有找到任何与此查询相关的解决方案。大多数链接建议只使用扩展/限制,但它们不起作用

请建议是否有任何解决方案以某种方式覆盖复杂类型。

回答

0

您可以使用jaxb2-simplify插件,特别是simplify:as-element-property扩展名,以便像您描述的那样生成对象。但是,如果你不使用maven,那么执行插件作为xjc命令行的扩展名似乎不实用,所以我试图用maven代替here