使用JAXB将Xml转换为Java对象

时间:2014-04-29 14:27:56

标签: java xml jaxb javax.xml

我需要将这个xml转换为Java Object,我使用的是手动DOM w3c(DocumentBuilder)。

但我会使用JAXB,

这是XML

<?xml version="1.0" encoding="UTF-8"?>
<user-agent>
    <style-class>{prefix} {prefix}-{0}</style-class>
    <template name="basic" default="true">/META-INF/page-basic.xhtml</template>
    <template name="frame">/META-INF/page-frame.xhtml</template>
    <support id="MSIE-OLD">
        <pattern>.*MSIE (4|5|6|7|8).*</pattern>
        <prefix>ie-old</prefix>
        <template name="no-support" default="true">/META-INF/no-support.xhtml</template>
        <style-class>no-support {prefix}</style-class>
    </support>
    <support id="MSIE">
        <pattern>.*MSIE (\d+).*</pattern>
        <prefix>ie</prefix>
    </support>
    <support id="Firefox">
        <pattern>.*Firefox/(\d+).*</pattern>
        <prefix>fx</prefix>
    </support>
    <support id="Android-Fx">
        <pattern>.*Firefox/(\d+).*Android/(\d+).*</pattern>
        <prefix>fx</prefix>
        <template name="basic" default="true">/META-INF/movil-basic.xhtml</template>
        <template name="frame">/META-INF/movil-frame.xhtml</template>
        <style-class>fx fx-{0} android-{1}</style-class>
    </support>
</user-agent>

这是我的java类:

public class Support implements Serializable {
  private final String id;
  private final Pattern pattern;
  private final String prefix;
  private final Map<String, String> templates = new HashMap();
  private final String defaultTemplate;
  private final String styleClass;
}

请帮帮我,我不知道如何使用默认属性和默认模板的情况,我尝试实现我的SupportAdapter(扩展XmlAdapter)

3 个答案:

答案 0 :(得分:0)

您可以通过创建两个类Support和MyTemplate来开始此示例     import java.io.Serializable;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;


@XmlAccessorType(XmlAccessType.FIELD)
public class Support implements Serializable {
    @XmlAttribute
    private String id;
    @XmlElement
    private String pattern;
    @XmlElement
    private String prefix;
    @XmlElement(name = "style-class")
    private String styleClass;
    @XmlElement(name = "template")
   private List<MyTemplate> templateList;


}

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;

public class MyTemplate  implements Serializable{
    @XmlAttribute
    private String name;
    @XmlAttribute(name = "default")
    private String defaultValue;
    @XmlValue
    private String value;

    public MyTemplate() {
        super();
    }
}

答案 1 :(得分:0)

你必须为它创建Jaxb pojos

<强> 1。 UserAgent

import java.util.ArrayList;
import java.util.List;
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 javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "styleClass", "template", "support" })
@XmlRootElement(name = "user-agent")
public class UserAgent {

    @XmlElement(name = "style-class", required = true)
    protected String styleClass;
    @XmlElement(required = true)
    protected List<Template> template;
    @XmlElement(required = true)
    protected List<Support> support;

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String value) {
        this.styleClass = value;
    }

    public List<Template> getTemplate() {
        if (template == null) {
            template = new ArrayList<Template>();
        }
        return this.template;
    }

    public List<Support> getSupport() {
        if (support == null) {
            support = new ArrayList<Support>();
        }
        return this.support;
    }

}

<强> 2。支持

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "pattern", "prefix", "template", "styleClass" })
@XmlRootElement(name = "support")
public class Support {

    @XmlElement(required = true)
    protected String pattern;
    @XmlElement(required = true)
    protected String prefix;
    protected List<Template> template;
    @XmlElement(name = "style-class")
    protected String styleClass;
    @XmlAttribute
    protected String id;

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String value) {
        this.pattern = value;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String value) {
        this.prefix = value;
    }

    public List<Template> getTemplate() {
        if (template == null) {
            template = new ArrayList<Template>();
        }
        return this.template;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String value) {
        this.styleClass = value;
    }

    public String getId() {
        return id;
    }

    public void setId(String value) {
        this.id = value;
    }
}

第3。模板

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "content" })
@XmlRootElement(name = "template")
public class Template {

    @XmlValue
    protected String content;
    @XmlAttribute(name = "default")
    protected String _default;
    @XmlAttribute
    protected String name;

    public String getContent() {
        return content;
    }

    public void setContent(String value) {
        this.content = value;
    }

    public String getDefault() {
        return _default;
    }

    public void setDefault(String value) {
        this._default = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

}

<强> 4。最后解析

JAXBContext jaxbContext = JAXBContext.newInstance(UserAgent.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
// pase file, url , inputstream , string whatever you have.
                UserAgent userAgent = (UserAgent) jaxbUnmarshaller.unmarshal(file);
                //getting <template> data from last <support> tag
                List<Template> templates = userAgent.getSupport().get(3).getTemplate();
                for (Template template : templates) {
                    String wantToSeeTempalteData = String.format(
                            "Tempalt [name:%s , default:%s content:%s ]",
                            new Object[] { template.getName(),template.getDefault(), template.getContent() });
                    System.out.println(wantToSeeTempalteData);
                }

<强>结果

Tempalt [name:basic , default:true content:/META-INF/movil-basic.xhtml ]
Tempalt [name:frame , default:null content:/META-INF/movil-frame.xhtml ]

答案 2 :(得分:-1)

使用XmlBeans http://xmlbeans.apache.org/

等框架

这将从xml到Java进行数据转换,反之亦然。