Xml node text value in java
How do I get JAXB to create a java class that will allow me to access the xml node text component that contains both text and elements?
Here's an example xml
:
<SomeClass someAttr="avalue">Text goes here.</SomeClass>
Following the unmarshalling
above xml
, I would like to be able to call the java method to return Text value goes here.
.
xsd
looks like this, please note that it uses mixed=true
:
<xs:complexType name="Some.Class" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="someclass" type="Some.Class"/>
<xs:element name="sub" type="Another.Class"/>
</xs:choice>
<xs:attribute name="someAttr" type="xs:NMTOKENS"/>
</xs:complexType>
This xsd calls JAXB to generate the following java, but I don't see a method to get the text:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Some.Class", propOrder = {"content"})
@Entity(name = "SomeClass")
@Table(name = "SOME_CLASS")
@Inheritance(strategy = InheritanceType.JOINED)
public class SomeClass implements Equals, HashCode {
@XmlElementRefs({
@XmlElementRef(name = "sub", namespace = "urn:some:namespace", type = JAXBElement.class),
@XmlElementRef(name = "someclass", namespace = "urn:some:namespace", type = JAXBElement.class)
})
@XmlMixed
protected List<Object> content;
@XmlAttribute
protected List<String> someAttr;
@XmlAttribute(name = "Hjid")
protected Long hjid;
protected transient List<SomeClass.SomeClassContentItem> contentItems;
protected transient List<SomeClass.SomeClassSomeAttrItem> someAttrItems;
@Transient
public List<Object> getContent() {
if (content == null) {content = new ArrayList<Object>();}
return this.content;
}
public void setContent(List<Object> content) {this.content = content;}
@Transient
public boolean isSetContent() {return ((this.content!= null)&&(!this.content.isEmpty()));}
public void unsetContent() {this.content = null;}
@Transient
public List<String> getSomeAttr() {
if (someAttr == null) {someAttr = new ArrayList<String>();}
return this.someAttr;
}
public void setSomeAttr(List<String> someAttr) {this.someAttr = someAttr;}
@Transient
public boolean isSetSomeAttr() {
return ((this.someAttr!= null)&&(!this.someAttr.isEmpty()));
}
public void unsetSomeAttr() {this.someAttr = null;}
@Id
@Column(name = "HJID")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getHjid() {return hjid;}
public void setHjid(Long value) {this.hjid = value;}
@OneToMany(targetEntity = SomeClass.SomeClassContentItem.class, cascade = {
CascadeType.ALL
}, fetch = FetchType.LAZY)
@JoinColumn(name = "CONTENT_ITEMS_SOME_CLASS_HJID")
public List<SomeClass.SomeClassContentItem> getContentItems() {
if (this.contentItems == null) {
this.contentItems = new ArrayList<SomeClass.SomeClassContentItem>();
}
if (MixedItemUtils.shouldBeWrapped(this.content)) {
this.content = MixedItemUtils.wrap(this.content, this.contentItems, SomeClass.SomeClassContentItem.class);
}
return this.contentItems;
}
public void setContentItems(List<SomeClass.SomeClassContentItem> value) {
this.content = null;
this.contentItems = null;
this.contentItems = value;
if (this.contentItems == null) {
this.contentItems = new ArrayList<SomeClass.SomeClassContentItem>();
}
if (MixedItemUtils.shouldBeWrapped(this.content)) {
this.content = MixedItemUtils.wrap(this.content, this.contentItems, SomeClass.SomeClassContentItem.class);
}
}
@OneToMany(targetEntity = SomeClass.SomeClassSomeAttrItem.class, cascade = {
CascadeType.ALL
}, fetch = FetchType.LAZY)
@JoinColumn(name = "SOME_ATTR_ITEMS_SOME_CLASS_H_0")
public List<SomeClass.SomeClassSomeAttrItem> getSomeAttrItems() {
if (this.someAttrItems == null) {
this.someAttrItems = new ArrayList<SomeClass.SomeClassSomeAttrItem>();
}
if (ItemUtils.shouldBeWrapped(this.someAttr)) {
this.someAttr = ItemUtils.wrap(this.someAttr, this.someAttrItems, SomeClass.SomeClassSomeAttrItem.class);
}
return this.someAttrItems;
}
public void setSomeAttrItems(List<SomeClass.SomeClassSomeAttrItem> value) {
this.someAttr = null;
this.someAttrItems = null;
this.someAttrItems = value;
if (this.someAttrItems == null) {
this.someAttrItems = new ArrayList<SomeClass.SomeClassSomeAttrItem>();
}
if (ItemUtils.shouldBeWrapped(this.someAttr)) {
this.someAttr = ItemUtils.wrap(this.someAttr, this.someAttrItems, SomeClass.SomeClassSomeAttrItem.class);
}
}
public boolean equals(ObjectLocator thisLocator, ObjectLocator thatLocator, Object object, EqualsStrategy strategy) {
if (!(object instanceof SomeClass)) {
return false;
}
if (this == object) {
return true;
}
final SomeClass that = ((SomeClass) object);
{
List<Object> lhsContent;
lhsContent = (this.isSetContent()?this.getContent():null);
List<Object> rhsContent;
rhsContent = (that.isSetContent()?that.getContent():null);
if (!strategy.equals(LocatorUtils.property(thisLocator, "content", lhsContent), LocatorUtils.property(thatLocator, "content", rhsContent), lhsContent, rhsContent)) {
return false;
}
}
{
List<String> lhsSomeAttr;
lhsSomeAttr = (this.isSetSomeAttr()?this.getSomeAttr():null);
List<String> rhsSomeAttr;
rhsSomeAttr = (that.isSetSomeAttr()?that.getSomeAttr():null);
if (!strategy.equals(LocatorUtils.property(thisLocator, "someAttr", lhsSomeAttr), LocatorUtils.property(thatLocator, "someAttr", rhsSomeAttr), lhsSomeAttr, rhsSomeAttr)) {
return false;
}
}
return true;
}
public boolean equals(Object object) {
final EqualsStrategy strategy = JAXBEqualsStrategy.INSTANCE;
return equals(null, null, object, strategy);
}
public int hashCode(ObjectLocator locator, HashCodeStrategy strategy) {
int currentHashCode = 1;
{
List<Object> theContent;
theContent = (this.isSetContent()?this.getContent():null);
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "content", theContent), currentHashCode, theContent);
}
{
List<String> theSomeAttr;
theSomeAttr = (this.isSetSomeAttr()?this.getSomeAttr():null);
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "someAttr", theSomeAttr), currentHashCode, theSomeAttr);
}
return currentHashCode;
}
public int hashCode() {
final HashCodeStrategy strategy = JAXBHashCodeStrategy.INSTANCE;
return this.hashCode(null, strategy);
}
@XmlAccessorType(XmlAccessType.FIELD)
@Entity(name = "SomeClass$SomeClassContentItem")
@Table(name = "SOME_CLASS_CONTENT_ITEM")
@Inheritance(strategy = InheritanceType.JOINED)
public static class SomeClassContentItem implements MixedItem<JAXBElement<?>> {
@XmlElementRefs({
@XmlElementRef(name = "someclass", namespace = "urn:some:namespace", type = JAXBElement.class),
@XmlElementRef(name = "sub", namespace = "urn:some:namespace", type = JAXBElement.class)
})
protected JAXBElement<?> item;
@XmlAttribute(name = "Text")
protected String text;
@XmlAttribute(name = "Hjid")
protected Long hjid;
@Transient
public JAXBElement<?> getItem() {return item;}
public void setItem(JAXBElement<?> value) {this.item = ((JAXBElement<?> ) value);}
@Basic
@Column(name = "TEXT")
@Lob
public String getText() {return text;}
public void setText(String value) {this.text = value;}
@Id
@Column(name = "HJID")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getHjid() {return hjid;}
public void setHjid(Long value) {this.hjid = value;}
@ManyToOne(targetEntity = SomeClass.class, cascade = {
CascadeType.ALL
}, fetch = FetchType.LAZY)
@JoinColumn(name = "ITEM_SOMECLASS_SOME_CLASS_CO_0")
public SomeClass getItemSomeclass() {
if (XmlAdapterUtils.isJAXBElement(SomeClass.class, new QName("urn:some:namespace", "someclass"), SomeClass.class, this.getItem())) {
return XmlAdapterUtils.unmarshallJAXBElement(((JAXBElement<? extends SomeClass> ) this.getItem()));
} else {return null;}
}
public void setItemSomeclass(SomeClass target) {
if (target!= null) {
setItem(XmlAdapterUtils.marshallJAXBElement(SomeClass.class, new QName("urn:some:namespace", "someclass"), SomeClass.class, target));
}
}
@ManyToOne(targetEntity = AnotherClass.class, cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "ITEM_SUB_SOME_CLASS_CONTENT__0")
public AnotherClass getItemSub() {
if (XmlAdapterUtils.isJAXBElement(AnotherClass.class, new QName("urn:some:namespace", "sub"), SomeClass.class, this.getItem())) {
return XmlAdapterUtils.unmarshallJAXBElement(((JAXBElement<? extends AnotherClass> ) this.getItem()));
} else {
return null;
}
}
public void setItemSub(AnotherClass target) {
if (target!= null) {
setItem(XmlAdapterUtils.marshallJAXBElement(AnotherClass.class, new QName("urn:some:namespace", "sub"), SomeClass.class, target));
}
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@Entity(name = "SomeClass$SomeClassSomeAttrItem")
@Table(name = "SOME_CLASS_SOME_ATTR_ITEM")
@Inheritance(strategy = InheritanceType.JOINED)
public static class SomeClassSomeAttrItem implements Item<String> {
@XmlElement(name = "SomeAttr")
@XmlSchemaType(name = "NMTOKENS")
protected String item;
@XmlAttribute(name = "Hjid")
protected Long hjid;
@Basic
@Column(name = "ITEM")
public String getItem() {return item;}
public void setItem(String value) {this.item = value;}
@Id
@Column(name = "HJID")
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getHjid() {return hjid;}
public void setHjid(Long value) {this.hjid = value;}
}
}
source to share
This xsd calls JAXB to generate the following java, but I don't see the way to get the text:
A property content
is a property that can contain text:
@XmlElementRefs({
@XmlElementRef(name = "sub", namespace = "urn:some:namespace", type = JAXBElement.class),
@XmlElementRef(name = "someclass", namespace = "urn:some:namespace", type = JAXBElement.class)
})
@XmlMixed
protected List<Object> content;
The reason is that because the type is Some.Class
mixed, you can have content like everything goes to List
:
<...>
a
<someclass/>
b
<sub/>
c
<sub/>
d
</...>
source to share