[英]Xml to Java Object using JAXB
我需要將此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)
您可以通過創建兩個類Support和MyTemplate開始此示例
導入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();
}
}
您必須為此創建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 ]
使用諸如XmlBeans http://xmlbeans.apache.org/之類的框架
這樣會將數據從xml轉換為Java,反之亦然。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.