我无法访问名称空间< str name =“" footprint'>与Jaxb [英] I can't access to the namespace <str name="footprint'> with Jaxb

查看:61
本文介绍了我无法访问名称空间< str name =“" footprint'>与Jaxb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部在标题中.我是Jaxb和XML的新手. 我可以访问<之类的其他名称空间.摘要>或< id>等...但是带有<的名称空间str name =">或< int name =">,我不能. 你能帮助我吗?我有点迷路了. 我所拥有的只是空数据,我找不到路.

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.

代码如下:

进食等级:

@XmlRootElement(name = "feed")
@XmlAccessorType(XmlAccessType.FIELD)
  public class Feed {

    @XmlElement(name = "entry")
    private List<Entry> entries;

    public List<Entry> getEntries() {
        return this.entries;
    }

}

入门班:

@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;
    }

}

XML:

第一部分

第二部分

谢谢!

推荐答案

您可以使用@XmlAttribute注释-因为您访问的数据是属性.属性具有名称和值-您的示例是:

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

在此示例中,属性的名称是name,其值是"footprint".

In this example the attribute's name is name and its value is "footprint".

因此注释必须为:

@XmlAttribute(name="name")

但是,由于XML包含多个name属性,因此JAXB会将每个属性创建为一个单独的对象-名称-值对的列表.

However, because your XML contains multiple name attributes, JAXB will create each one as a separate object - a list of name-value pairs.

对于XML的以下简化表示...

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:

提要类

@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;
    }

}

然后,解组看起来像这样:

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);

这将创建一个StrType对象的列表,如下所示:

This creates a list of StrType objects, like this:

显然,这与您要在问题中创建的结构不同.但是会捕获所有数据,并根据要求使用jaxb.

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.

可能有其他方法可以直接支持您的bean布局-例如,一种StAX方法,您可以在逐个标签扫描XML时填充每个bean.它本身具有一些明显的缺点(例如,手动装豆).

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

这篇关于我无法访问名称空间&lt; str name =“&quot; footprint'&gt;与Jaxb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆