问题
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:
first part
second part
Thank you !
回答1:
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:
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).
来源:https://stackoverflow.com/questions/61012517/i-cant-access-to-the-namespace-str-name-footprint-with-jaxb