从XML字符串到Java对象的JAXB,出了什么问题?

时间:2012-07-26 14:56:44

标签: java xml jaxb

我想从XML创建一些对象,但是当我尝试时我得到了这个错误:

[26/07/12 16:20:03:763 CEST] ERROR sitemap.SitemapXMLServlet:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.sitemaps.org/schemas/sitemap/0.9", local:"urlset"). Expected elements are <{}url>
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.sitemaps.org/schemas/sitemap/0.9", local:"urlset"). Expected elements are <{}url>

我找不到问题,我不明白为什么会出错。这就是我试过的:

XML文件

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://LINK/shop/hoofdcategorie</loc>
        <lastmod>2012-07-26</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
    <url>
        <loc>
            http://LINK/shop/hoofdcategorie/subcategorie
        </loc>
        <lastmod>2012-07-26</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
    </url>
</urlset>

将XML解组为对象的方法

public void getXML() {
    final JAXBContext context = JAXBContext.newInstance(SitemapXML.class);
    final Unmarshaller unmarshaller = context.createUnmarshaller();
    String xml = URLReader.readUrlHttpClient("http://LINK/shop/sitemap.xml");
    final UrlSet urlSet = (UrlSet) unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
}

UrlSet类

@XmlRootElement(name = "urlset")
public class UrlSet {

    @XmlAttribute
    String xmlns;
    @XmlElement(name = "url")
    ArrayList<SitemapXML> sitemaps;


    public ArrayList<SitemapXML> getSitemaps() {
        return sitemaps;
    }

    public void setSitemaps(ArrayList<SitemapXML> sitemaps) {
        this.sitemaps = sitemaps;
    }

    @XmlAttribute
    public String getXmlns() {
        return xmlns;
    }

    public void setXmlns(String xmlns) {
        this.xmlns = xmlns;
    }
}

SitemapXML类我映射到url的内容

@XmlRootElement(name = "url")
public class SitemapXML {

    String loc;
    Date lastmod;
    String changefreq;
    Double priority;

    public String getLoc() {
        return loc;
    }
    public void setLoc(String loc) {
        this.loc = loc;
    }
    public Date getLastmod() {
        return lastmod;
    }
    public void setLastmod(Date lastmod) {
        this.lastmod = lastmod;
    }
    public String getChangefreq() {
        return changefreq;
    }
    public void setChangefreq(String changefreq) {
        this.changefreq = changefreq;
    }
    public Double getPriority() {
        return priority;
    }
    public void setPriority(Double priority) {
        this.priority = priority;
    }
}

2 个答案:

答案 0 :(得分:1)

您的xml使用命名空间,但您的注释未提及命名空间。您可以在XmlRootElement批注中指定命名空间,或在包级别添加XmlSchema批注。

答案 1 :(得分:0)

您应该使用package-info注释添加@XmlSchema类以指定命名空间限定。

<强>包信息

以下是带有必要package-info注释的示例@XmlSchema类。您需要更改包以匹配您的域对象。

@XmlSchema(
    namespace = "http://www.sitemaps.org/schemas/sitemap/0.9",
    elementFormDefault = XmlNsForm.QUALIFIED)
package com.example.foo;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

了解更多信息