root xml元素中的jaxb前缀ns2

时间:2015-10-19 20:56:43

标签: java xml jaxb

我有一个问题。 我想创建站点地图,但在根元素上我有<ns2:urlset>

我想删除ns2,因此我创建了package-info:

    @XmlSchema(
        namespace = "http://www.something.com/something",
        elementFormDefault = XmlNsForm.QUALIFIED)

package app.sitemaps;


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

我有两个类来生成站点地图:

    package app.sitemaps;


import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collection;
;



@XmlAccessorType(value = XmlAccessType.NONE)
@XmlRootElement(name = "urlset", namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")
public class XmlUrlSet {

    @XmlElements({@XmlElement(name = "url", type = XmlUrl.class)})
    private Collection<XmlUrl> xmlUrls = new ArrayList<XmlUrl>();

    public void addUrl(XmlUrl xmlUrl) {
        xmlUrls.add(xmlUrl);
    }

    public Collection<XmlUrl> getXmlUrls() {
        return xmlUrls;
    }

}

package app.sitemaps;


import org.springframework.format.annotation.DateTimeFormat;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.text.SimpleDateFormat;
import java.util.Date;



@XmlAccessorType(value = XmlAccessType.NONE)
@XmlRootElement(name = "url")
public class XmlUrl {

    public enum Priority {
        HIGH("1.0"), MEDIUM("0.5");

        private String value;

        Priority(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    @XmlElement
    private String loc;

    @XmlElement
    private String lastmod = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

    @XmlElement
    private String changefreq = "daily";

    @XmlElement
    private String priority;

    public XmlUrl() {
    }

    public XmlUrl(String loc, Priority priority) {
        this.loc = loc;
        this.priority = priority.getValue();
    }

    public String getLoc() {
        return loc;
    }

    public String getPriority() {
        return priority;
    }

    public String getChangefreq() {
        return changefreq;
    }

    public String getLastmod() {
        return lastmod;
    }

}

结果:

  <ns2:urlset>
<url>
<loc>http://www......</loc>
<lastmod>2015-10-19</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
</ns2:urlset>

0 个答案:

没有答案