我无法访问带有Jaxb的命名空间

huangapple go评论93阅读模式
英文:

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 = &quot;feed&quot;)
@XmlAccessorType(XmlAccessType.FIELD)
  public class Feed {

    @XmlElement(name = &quot;entry&quot;)
    private List&lt;Entry&gt; entries;

    public List&lt;Entry&gt; getEntries() {
        return this.entries;
    }

}

Entry Class:

@XmlRootElement(name = &quot;entry&quot;)
@XmlAccessorType(XmlAccessType.FIELD)
public class Entry {
    //@XmlElement(name = &quot;footprint&quot;)//&lt;str name=&quot;footprint&quot;&gt; dont work.
    //@XmlAttribute(name=&quot;footprint&quot;)//dont work
    //@XmlValue//dont work
    private String footprint;

    @XmlElement(name = &quot;uuid&quot;)
    private String id;

    @XmlElement(name = &quot;size&quot;)
    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

得分: 0

你可以使用@XmlAttribute注解,因为你要访问的数据是属性。属性有一个名称和一个值 - 你的示例是:

&lt;str name=&quot;footprint&quot;&gt;

在这个示例中,属性的名称是name,其值是&quot;footprint&quot;

所以注解应该是:

@XmlAttribute(name=&quot;name&quot;)

然而,因为你的XML包含多个name属性,JAXB会将每个属性创建为一个单独的对象 - 一个名称-值对的列表。

对于你的XML的以下精简表示...

&lt;feed&gt;
  &lt;entry&gt;
    &lt;str name=&quot;footprint&quot;&gt;这里有一些非常长的数据字符串。&lt;/str&gt;
    &lt;str name=&quot;format&quot;&gt;SAFE&lt;/str&gt;
  &lt;/entry&gt;
&lt;/feed&gt;

... 我们有以下三个相关的类:

feed类

@XmlRootElement(name = &quot;feed&quot;)
@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 = &quot;entry&quot;)
public class EntryType {

    @XmlElement(required = true)
    protected List&lt;StrType&gt; str;

    public List&lt;StrType&gt; getStr() {
        if (str == null) {
            str = new ArrayList&lt;&gt;();
        }
        return this.str;
    }

}

Str类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = &quot;str&quot;)
public class StrType {

    @XmlAttribute(name = &quot;name&quot;)
    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(&quot;path/to/your/sample.xml&quot;);
FeedType feed = (FeedType) unmarshaller.unmarshal(xml);

这会创建一个StrType对象的列表,像这样:
我无法访问带有Jaxb的命名空间<str name=。" 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:

&lt;str name=&quot;footprint&quot;&gt;

In this example the attribute's name is name and its value is &quot;footprint&quot;.

So the annotation needs to be:

@XmlAttribute(name=&quot;name&quot;)

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

&lt;feed&gt;
  &lt;entry&gt;
    &lt;str name=&quot;footprint&quot;&gt;Some very long string of data in here.&lt;/str&gt;
    &lt;str name=&quot;format&quot;&gt;SAFE&lt;/str&gt;
  &lt;/entry&gt;
&lt;/feed&gt;

... we have the following three related classes:

The feed class

@XmlRootElement(name = &quot;feed&quot;)
@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 = &quot;entry&quot;)
public class EntryType {

    @XmlElement(required = true)
    protected List&lt;StrType&gt; str;

    public List&lt;StrType&gt; getStr() {
        if (str == null) {
            str = new ArrayList&lt;&gt;();
        }
        return this.str;
    }

}

The Str class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = &quot;str&quot;)
public class StrType {

    @XmlAttribute(name = &quot;name&quot;)
    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(&quot;path/to/your/sample.xml&quot;);
FeedType feed = (FeedType) unmarshaller.unmarshal(xml);

This creates a list of StrType objects, like this:
我无法访问带有Jaxb的命名空间<str name=。" 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).

huangapple
  • 本文由 发表于 2020年4月3日 21:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61012517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定