将xml节点的文本值转换为java
如何让JAXB生成一个java类,允许我访问包含文本和元素的xml节点的文本组件? 下面是将xml节点的文本值转换为java,java,xml,jaxb,xsd,Java,Xml,Jaxb,Xsd,如何让JAXB生成一个java类,允许我访问包含文本和元素的xml节点的文本组件? 下面是xml的一个示例: <SomeClass someAttr="avalue">Text goes here.</SomeClass> <xs:complexType name="Some.Class" mixed="true"> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:elem
xml
的一个示例:
<SomeClass someAttr="avalue">Text goes here.</SomeClass>
<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>
这个xsd导致JAXB生成以下java,但我看不到获取文本的方法:
@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;}
}
}
@xmlacessortype(xmlacesstype.FIELD)
@XmlType(name=“Some.Class”,propOrder={“content”})
@实体(name=“SomeClass”)
@表(name=“某些类”)
@继承(策略=InheritanceType.JOINED)
公共类SomeClass实现了Equals,HashCode{
@XmlElementRefs({
@xmlementref(name=“sub”,namespace=“urn:some:namespace”,type=JAXBElement.class),
@xmlementref(name=“someclass”,namespace=“urn:some:namespace”,type=JAXBElement.class)
})
@混合
受保护的列表内容;
@XmlAttribute
受保护列表somettr;
@xmltattribute(name=“Hjid”)
保护长hjid;
受保护的临时列表内容项;
受保护的临时清单项目;
@短暂的
公共列表getContent(){
如果(content==null){content=new ArrayList();}
返回此.content;
}
public void setContent(列表内容){this.content=content;}
@短暂的
公共布尔值isSetContent(){return((this.content!=null)&&(!this.content.isEmpty());}
公共内容(){this.content=null;}
@短暂的
公共列表getSomeAttr(){
如果(someAttr==null){someAttr=newArrayList();}
返回此.someAttr;
}
public void setSomeAttr(List someAttr){this.someAttr=someAttr;}
@短暂的
公共布尔值isSetSomeAttr(){
返回((this.someAttr!=null)&(!this.someAttr.isEmpty());
}
public void unsetomeattr(){this.someAttr=null;}
@身份证
@列(name=“HJID”)
@GeneratedValue(策略=GenerationType.AUTO)
public Long getHjid(){return hjid;}
public void setHjid(长值){this.hjid=value;}
@OneToMany(targetEntity=SomeClass.SomeClassContentItem.class,级联={
全部
},fetch=FetchType.LAZY)
@JoinColumn(name=“CONTENT\u ITEMS\u SOME\u CLASS\u HJID”)
公共列表getContentItems(){
if(this.contentItems==null){
this.contentItems=新的ArrayList();
}
if(MixedItemUtils.shouldBeWrapped(this.content)){
this.content=MixedItemUtils.wrap(this.content,this.contentItems,SomeClass.SomeClassContentItem.class);
}
返回此.contentItems;
}
public void setContentItems(列表值){
this.content=null;
this.contentItems=null;
this.contentItems=值;
if(this.contentItems==null){
this.contentItems=新的ArrayList();
}
if(MixedItemUtils.shouldBeWrapped(this.content)){
this.content=MixedItemUtils.wrap(this.content,this.contentItems,SomeClass.SomeClassContentItem.class);
}
}
@OneToMany(targetEntity=SomeClass.SomeClassSomeAttrItem.class,级联={
全部
},fetch=FetchType.LAZY)
@JoinColumn(name=“某些属性项某些类”
公共列表getSomeAttrItems(){
if(this.someAttrItems==null){
this.someAttrItems=new ArrayList();
}
if(ItemUtils.shouldBeWrapped(this.someAttr)){
this.someAttr=ItemUtils.wrap(this.someAttr,this.someAttrItems,SomeClass.SomeClassSomeAttrItem.class);
}
返回此.someAttrItems;
}
public void setSomeAttrItems(列表值){
this.someAttr=null;
this.someAttrItems=null;
this.someAttrItems=值;
if(this.someAttrItems==null){
this.someAttrItems=new ArrayList();
}
if(ItemUtils.shouldBeWrapped(this.someAttr)){
this.someAttr=ItemUtils.wrap(this.someAttr,this.someAttrItems,SomeClass.SomeClassSomeAttrItem.class);
}
}
公共布尔等于(ObjectLocator thisLocator,ObjectLocator thatLocator,Object Object,EqualsStrategy){
if(!(SomeClass的对象实例)){
返回false;
}
if(this==对象){
返回true;
}
最后一个SomeClass=((SomeClass)对象);
{
列出LHS内容;
lhsContent=(this.isSetContent()?this.getContent():null);
列出rhsContent;
rhsContent=(that.isSetContent()?that.getContent():null);
如果(!strategy.equals(LocatorUtils.property(thisLocator,“content”,lhsContent),LocatorUtils.property(thatLocator,“content”,rhsContent),lhsContent,rhsContent)){
返回false;
}
}
{
列表lhsSomeAttr;
lhsSomeAttr=(this.isSetSomeAttr()?this.getSomeAttr():null);
列出rhsSomeAttr;
rhsSomeAttr=(that.isSetSomeAttr()?that.getSomeAttr():null);
if(!strategy.equals(LocatorUtils.property(thisLocator,“someAttr”,lhsSomeAttr),LocatorUtils.property(thatLocator,“someAttr”,RHSOMEATTR),lhsSomeAttr,RHSOMEATTR)){
返回false;
}
}
返回true;
}
公共布尔等于(对象){
final equalstrategy=jaxbequalstrategy.INSTANCE;
返回等于(null、null、object、strategy);
}
public int hashCode(ObjectLocator定位器、hashCode策略){
int currentHashCode=1;
{
列出内容;
内容=(this.isSetContent()?this.getContent():null);
currentHashCode=strategy.hashCode(LocatorUtils.property(locator,“content”,the content),currentHashCode,the content);
}
{
列出一些属性;
theSomeAttr=(this.isSetSomeAttr()?this.getSomeAttr():null);
currentHashCode=strategy.hashCode(LocatorUtils.property
@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;
<...>
a
<someclass/>
b
<sub/>
c
<sub/>
d
</...>