英文:
I can't access to the namespace <str name="footprint'> with Jaxb
问题
以下是翻译好的内容:
Feed类:
@XmlRootElement(name = "feed")
@XmlAccessorType(XmlAccessType.FIELD)
public class Feed {
@XmlElement(name = "entry")
private List<Entry> entries;
public List<Entry> getEntries() {
return this.entries;
}
}
Entry类:
@XmlRootElement(name = "entry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {
//@XmlElement(name = "footprint")//<str name="footprint">不起作用。
//@XmlAttribute(name="footprint")//不起作用
//@XmlValue//不起作用
private String footprint;
@XmlElement(name = "uuid")
private String id;
@XmlElement(name = "size")
private String size;
public Entry() {}
public String getCoordinates() {
return footprint;
}
public String getId() {
return id;
}
public String getSize() {
return size;
}
public void setCoordinates(String footprint) {
this.footprint=footprint;
}
public void setSize(String size) {
this.size=size;
}
public void setId(String id) {
this.id=id;
}
}
XML内容:
谢谢!
英文:
All is in the title. I'm new with Jaxb and the XML stuff.
I can acces to others namespaces like < summary> or < id> etc... But namespaces with < str name=""> or < int name="">, i can't.
Can you help me? I'm a little lost.
All i have is null data, i don't find the way.
Here's the code:
Feed class:
@XmlRootElement(name = "feed")
@XmlAccessorType(XmlAccessType.FIELD)
public class Feed {
@XmlElement(name = "entry")
private List<Entry> entries;
public List<Entry> getEntries() {
return this.entries;
}
}
Entry Class:
@XmlRootElement(name = "entry")
@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {
//@XmlElement(name = "footprint")//<str name="footprint"> dont work.
//@XmlAttribute(name="footprint")//dont work
//@XmlValue//dont work
private String footprint;
@XmlElement(name = "uuid")
private String id;
@XmlElement(name = "size")
private String size;
public Entry() {}
public String getCoordinates() {
return footprint;
}
public String getId() {
return id;
}
public String getSize() {
return size;
}
public void setCoordinates(String footprint) {
this.footprint=footprint;
}
public void setSize(String size) {
this.size=size;
}
public void setId(String id) {
this.id=id;
}
}
The XML:
Thank you !
答案1
得分: 0
你可以使用@XmlAttribute
注解,因为你要访问的数据是属性。属性有一个名称和一个值 - 你的示例是:
<str name="footprint">
在这个示例中,属性的名称是name
,其值是"footprint"
。
所以注解应该是:
@XmlAttribute(name="name")
然而,因为你的XML包含多个name
属性,JAXB会将每个属性创建为一个单独的对象 - 一个名称-值对的列表。
对于你的XML的以下精简表示...
<feed>
<entry>
<str name="footprint">这里有一些非常长的数据字符串。</str>
<str name="format">SAFE</str>
</entry>
</feed>
... 我们有以下三个相关的类:
feed类
@XmlRootElement(name = "feed")
@XmlAccessorType(XmlAccessType.FIELD)
public class FeedType {
@XmlElement(required = true)
protected EntryType entry;
public EntryType getEntry() {
return entry;
}
public void setEntry(EntryType value) {
this.entry = value;
}
}
Entry类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "entry")
public class EntryType {
@XmlElement(required = true)
protected List<StrType> str;
public List<StrType> getStr() {
if (str == null) {
str = new ArrayList<>();
}
return this.str;
}
}
Str类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "str")
public class StrType {
@XmlAttribute(name = "name")
private String name;
@XmlValue
private String data;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
然后,解组看起来像这样:
JAXBContext jc = JAXBContext.newInstance(FeedType.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("path/to/your/sample.xml");
FeedType feed = (FeedType) unmarshaller.unmarshal(xml);
这会创建一个StrType对象的列表,像这样:
。" decoding="async" src="https://i.stack.imgur.com/RybP1.png" alt="enter image description here" />
这显然是一个与你在问题中想要创建的结构不同的结构。但是所有的数据都被捕获了,而且使用了jaxb,如你所请求。
可能会有一些替代方法可以直接支持你的bean布局 - 例如,一种StAX方法,你可以在扫描XML时逐个标签地填充每个bean。这也有一些明显的劣势(例如,手动填充bean)。
英文:
You can use the @XmlAttribute
annotation - since the data you are accessing are attributes. An attribute has a name and a value - your example is:
<str name="footprint">
In this example the attribute's name is name
and its value is "footprint"
.
So the annotation needs to be:
@XmlAttribute(name="name")
However, because your XML contains multiple name
attributes, JAXB will create each one as a separate object - a list of name-value pairs.
For the following stripped-down representation of your XML...
<feed>
<entry>
<str name="footprint">Some very long string of data in here.</str>
<str name="format">SAFE</str>
</entry>
</feed>
... we have the following three related classes:
The feed class
@XmlRootElement(name = "feed")
@XmlAccessorType(XmlAccessType.FIELD)
public class FeedType {
@XmlElement(required = true)
protected EntryType entry;
public EntryType getEntry() {
return entry;
}
public void setEntry(EntryType value) {
this.entry = value;
}
}
The Entry class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "entry")
public class EntryType {
@XmlElement(required = true)
protected List<StrType> str;
public List<StrType> getStr() {
if (str == null) {
str = new ArrayList<>();
}
return this.str;
}
}
The Str class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "str")
public class StrType {
@XmlAttribute(name = "name")
private String name;
@XmlValue
private String data;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
Then, unmarshalling looks like this:
JAXBContext jc = JAXBContext.newInstance(FeedType.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("path/to/your/sample.xml");
FeedType feed = (FeedType) unmarshaller.unmarshal(xml);
This creates a list of StrType objects, like this:
。" decoding="async" src="https://i.stack.imgur.com/RybP1.png" alt="enter image description here" />
This is obviously a different structure than the one you are looking to create in your question. But all the data is captured, and it uses jaxb, as requested.
There may be alternative approaches which might support your bean layout directly - for example, a StAX approach where you populate each bean as the XML is scanned tag-by-tag. That has some obvious disadvantages of its own (manual bean population, for example).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论