Monday, 29 September 2014

extracting element text value



I am using JAXB to generate code for unmarshalling xml into java entities using an xsd file for schematics. The problem is that the resulting code does not generate the name of an organization specified in the following xml:



<organization>
<name>Some organization's name goes here</name>
</organization>


Here is the xsd definition for the Organization data type:



<xs:complexType name="Organization">
<xs:sequence>
<xs:element name="name" type="ON" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="classCode" type="EntityClassOrganization" use="optional" fixed="ORG"/>
</xs:complexType>


Here is the xsd definition for the ON datatype:



<xs:complexType name="ON" mixed="true">
<xs:annotation>
<xs:documentation>
A name for an organization. A sequence of name parts.
</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="delimiter" type="en.delimiter"/>
<xs:element name="prefix" type="en.prefix"/>
<xs:element name="suffix" type="en.suffix"/>
</xs:sequence>
<xs:attribute name="use" use="optional" type="set_EntityNameUse">
<xs:annotation>
<xs:documentation>
A set of codes advising a system or user which name
in a set of like names to select for a given purpose.
A name without specific use code might be a default
name useful for any purpose, but a name with a specific
use code would be preferred for that respective purpose.
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>


Here is the resulting java code created by JAXB:



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

@XmlElementRefs({
@XmlElementRef(name = "delimiter", namespace = "urn:something", type = JAXBElement.class),
@XmlElementRef(name = "prefix", namespace = "urn:something", type = JAXBElement.class),
@XmlElementRef(name = "suffix", namespace = "urn:something", type = JAXBElement.class)
})
@XmlMixed
protected List<Serializable> content;
@XmlAttribute(name = "use")
protected List<String> use;

public List<Serializable> getContent() {
if (content == null) {content = new ArrayList<Serializable>();}
return this.content;
}

public List<String> getUse() {
if (use == null) {use = new ArrayList<String>();}
return this.use;
}
}


There are several problems with this resulting java class. First off, it creates List<Serializable> content; instead of creating separate properties for delimiter, prefix, and suffix. And just as importantly, it also fails to give me access to the text value inside the name tags in the xml at top. When I remove the mixed="true" from the ON definition in the xsd file, the content list is replaced with separate properties for the delimiter, prefix, and suffix, but I still can't get the text content of the name element. I hesitate to remove mixed=true because I read that mixed=true specified that a complextype can contain elements, attributes, and text.


How can I change the code above so that is generates a method for retrieving the text of the name element in addition to generating separate methods for each of the other elements/properties?


No comments:

Post a Comment