你必须为它创建 Jaxb pojos
1. 用户代理
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 ]