Danny Gloudemans
Danny Gloudemans

Reputation: 2697

JAXB from XML string to Java object, what is going wrong?

I want to create from XML some objects, but when I'm trying I get this error:

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

I can't find the problem, and I don't understand why it is getting wrong. This is what I tried:

XML File

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

Method what unmarshal the XML to an Object

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 Class

@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 class what I mapping to 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;
    }
}

Upvotes: 1

Views: 1206

Answers (2)

bdoughan
bdoughan

Reputation: 149047

You should add a package-info class with the the @XmlSchema annotation to specify the namespace qualification.

package-info

Below is a sample package-info class with the necessary @XmlSchema annotation. You will need to change the package to match your domain objects.

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

For More Information

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

your xml uses a namespace, but your annotations don't mention the namespaces. you can specify the namespace in the XmlRootElement annotation or add an XmlSchema annotation at the package level.

Upvotes: 1

Related Questions