Retrieving the text value of an element
I am using JAXB
to generate code for unmarshalling xml in java entity using file xsd
for schemas. The problem is that the resulting code does not generate name
for the organization
one specified in the following xml:
<organization>
<name>Some organization name goes here</name>
</organization>
Here is the xsd definition for the datatype organization
:
<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 definition xsd
for the data type ON
:
<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 generated 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;
}
}
As a result of this java class, several problems arise. First of all, it creates List<Serializable> content;
instead of creating separate properties for delimiter
, prefix
and suffix
. And more importantly, it also prevents me from accessing the text value inside the tags name
in the xml from above. When I remove mixed="true"
from the definition ON
in the xsd file, the content list is replaced with separate properties for delimiter
, prefix
and suffix
, but I still cannot get the text of the element's content name
. I am hesitant to remove mixed=true
because I read that mixed = true indicated what it complextype
might contain elements
, attributes
and text
.
How can I modify the above code to generate a method to retrieve the text of an element name
in addition to generating separate methods for each of the other elements / properties?
source to share
Try Simplify plugin . I'm not entirely sure if it does what you want, but it is written for a similar case.
Example:
<xs:complexType name="typeWithElementsProperty">
<xs:choice maxOccurs="unbounded">
<xs:element name="a" type="xs:string"/>
<xs:element name="b" type="xs:int"/>
</xs:choice>
</xs:complexType>
Gives you:
@XmlElements({
@XmlElement(name = "a", type = String.class)
@XmlElement(name = "b", type = Integer.class),
})
protected List<Serializable> aOrB;
But with a simplified plugin:
<xs:complexType name="typeWithElementsProperty">
<xs:choice maxOccurs="unbounded">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
<xs:element name="a" type="xs:string"/>
<xs:element name="b" type="xs:int"/>
</xs:choice>
</xs:complexType>
You'll get:
@XmlElement(name = "a", type = String.class)
protected List<String> a;
@XmlElement(name = "b", type = Integer.class)
protected List<Integer> b;
You have a somewhat similar case, you get your property from behind mixed="true"
.
If that doesn't work for mixed OOTB types, send me a PR with your test case here and post a question here .
UPDATE
I have implemented this feature .
From this :
<xs:complexType name="gh1" mixed="true">
<xs:sequence>
<xs:element name="a" type="xs:string">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="b" type="xs:int"/>
</xs:sequence>
</xs:complexType>
You will get this:
protected List<String> a;
@XmlElement(type = Integer.class)
protected List<Integer> b;
@XmlMixed
protected List<String> content;
Will be in the next version (0.9.0).
source to share