将XML节点的文本值转换为Java

3

如何让JAXB生成一个Java类,以便可以访问同时包含文本和元素的XML节点的文本组件?

以下是xml示例:

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

在解组上述xml后,我希望能够调用Java方法返回“文本值在此处”的文本。下面是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个回答

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
</...>

谢谢。问题可能是我的模式使用单词"content"作为定义属性名称,这可能会覆盖你所说的内容。让我琢磨一下我的代码,然后很快回复你。 - CodeMed
我正在 Eclipse 中查看这个。我看到内容属性中的对象列表。但是,我应该使用什么 Java 语法从内容属性中提取文本值? - CodeMed
List中,您将拥有JAXBElementString实例的值。 String实例是您的文本值。 - bdoughan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接