gpt4 book ai didi

java - xml节点的文本值到java

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:25 26 4
gpt4 key购买 nike

如何让 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;}

}

}

最佳答案

This xsd is causing JAXB to generate the following java, but I do not see the method for getting the text:

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

关于java - xml节点的文本值到java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27002826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com