将xml节点的文本值转换为java

时间:2014-11-18 19:52:43

标签: java xml jaxb xsd

如何让JAXB生成一个java类,允许我访问包含文本和元素的xml节点的文本组件?

以下是xml

的示例
<SomeClass someAttr="avalue">Text goes here.</SomeClass>

在上述unmarshalling xml之后,我希望能够调用java方法返回Text value goes here.

xsd如下,请注意它使用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>

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

    }

}

1 个答案:

答案 0 :(得分:1)

  

这个xsd导致JAXB生成以下java,但我没有   看到获取文本的方法:

content属性是可以包含文本的属性:

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

原因是,由于Some.Class类型是混合的,您可以拥有类似的内容,所有这些内容都在List中:

<...>
    a
    <someclass/>
    b
    <sub/>
    c
    <sub/>
    d
</...>