0

I have an XSD file which is provided by a third party vendor. I need to parse that XSD file and generate Java objects. I am using JAXB to for parsing XSD file through maven plugin.

Everything was going smoothly until i recently got a requirement to use data from one of the tags from the XML being parsed. The complexType for the tag has mixed=true due to which the java class generated by JAXB is as below.

XSD Complex Type:

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

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

Instead of having a List<GeneralRemark>, GeneralRemarks class contains List<Serializable>.

I have searched a lot on how to fetch data from List<Serializable> and found the solution to use the below code to extract data.

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());
  }

}

I want to know if it is possible to somehow override the ComplexType in a chile xsd or a binding file(.xjc) to either redefine the ComplexType or somehow change mixed attribute value to false so that correct classes are generated.

I have tried to use Extend/Restrict in my xsd. Also tried to create a custom type and extend the parentXML node which has GeneralRemarks type element to use my custom complex type in child xsd but all these didn't work either.

I've tried googling a lot but didn't find any solution relevant to this query. Most of the links suggested only to use either Extend/Restrict but they didn't work

Please suggest if there is any solution to override the complex type somehow.

Raghav
  • 552
  • 1
  • 9
  • 34

1 Answers1

0

You can use jaxb2-simplify plugin especially simplify:as-element-property extension to generate objects like the way you described. However if you dont use maven, then executing the plugin as extension of xjc command line seems not practical, so I tried to use maven instead as shown here

Community
  • 1
  • 1
ulab
  • 1,079
  • 3
  • 15
  • 45