您的位置:首页 > 编程语言 > Java开发

Java对下面XML文档解析要求封装为两个类

2014-03-21 00:01 513 查看
mport javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder={"dbtype","url","user", "password"})
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="datasource")
public class Datasource {
@XmlElement(name="db-type")
private String dbtype;

@XmlElement(name="url")
private String url;

@XmlElement(name="user")
private String user;

@XmlElement(name="password")
private String password;

public Datasource() {
super();
}

public Datasource(String dbtype, String url, String user, String password) {
super();
this.dbtype = dbtype;
this.url = url;
this.user = user;
this.password = password;
}

public String getDbtype() {
return dbtype;
}

public void setDbtype(String dbtype) {
this.dbtype = dbtype;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "Datasource [dbtype=" + dbtype + ", url=" + url + ", user="
+ user + ", password=" + password + "]";
}
}

import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder={"sql","time","column","allyCode"})
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="news-ws")
public class NewsWs {
@XmlElement(name="sql")
private String sql;

@XmlElement(name="time")
private String time;

@XmlElement(name="column")
private String column;

@XmlElement(name="ally-code")
private String allyCode;

public NewsWs() {
super();
}

public NewsWs(String sql, String time, String column, String allyCode) {
super();
this.sql = sql;
this.time = time;
this.column = column;
this.allyCode = allyCode;
}

public String getSql() {
return sql;
}

public void setSql(String sql) {
this.sql = sql;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getColumn() {
return column;
}

public void setColumn(String column) {
this.column = column;
}

public String getAllyCode() {
return allyCode;
}

public void setAllyCode(String allyCode) {
this.allyCode = allyCode;
}

@Override
public String toString() {
return "NewsWs [sql=" + sql + ", time=" + time + ", column=" + column
+ ", allyCode=" + allyCode + "]";
}
}

import java.util.Set;

import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Context", propOrder = { "datasource", "newsWs" })
@XmlRootElement(name = "context")
public class Context {
@XmlElement(name="datasource")
private Datasource datasource;

@XmlElementWrapper(name="news-ws-define")
@XmlElement(name ="news-ws")
private Set<NewsWs> newsWs;

public Context() {
super();
}

public Context(Datasource datasource, Set<NewsWs> newsWs) {
super();
this.datasource = datasource;
this.newsWs = newsWs;
}

public Datasource getDatasource() {
return datasource;
}

public void setDatasource(Datasource datasource) {
this.datasource = datasource;
}

public Set<NewsWs> getNewsWs() {
return newsWs;
}

public void setNewsWs(Set<NewsWs> newsWs) {
this.newsWs = newsWs;
}

@Override
public String toString() {
return "Conext [datasource=" + datasource + ", newsWs=" + newsWs + "]";
}
}

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashSet;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class Test {
public static void main(String[] args) throws Exception {
Set<NewsWs> set = new HashSet<NewsWs>();

Datasource datasource = new Datasource("Mysql",
"http://127.0.0.1:8080/Hessian/hello", "root", "123");

NewsWs newsWs1 = new NewsWs("sql1", "2013年7月13日", "新闻内容1", "验证1");
NewsWs newsWs2 = new NewsWs("sql2", "2013年7月14日", "新闻内容2", "");
NewsWs newsWs3 = new NewsWs("sql3", "2013年7月15日", "新闻内容3", "验证1");

set.add(newsWs1);
set.add(newsWs2);
set.add(newsWs3);

Context context = new Context(datasource, set);

JAXBContext jaxbContext = JAXBContext.newInstance(context.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
jaxbMarshaller.marshal(context, byteArrayOutputStream);

String xml = byteArrayOutputStream.toString("UTF-8");
System.out.println(xml);

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<Context> jaxbElement = (JAXBElement<Context>) unmarshaller
.unmarshal(new StreamSource(byteArrayInputStream),
Context.class);
Context context1 = (Context) jaxbElement.getValue();

System.out.println(context1.getDatasource().toString());
Set<NewsWs> set1 = context1.getNewsWs();
for(NewsWs ws : set1) {
System.out.println("**************");
System.out.println(ws.getSql());
System.out.println(ws.getTime());
System.out.println(ws.getColumn());
System.out.println(ws.getAllyCode());
}
}
}

代码里面我用了Set导致有时生成的xml文件中tag<news-ws>顺序打乱了,如果想按集合添加顺序来生成xml,只需要把Set用List替换即可。
@XmlElementWrapper(name="news-ws-define")
@XmlElement(name ="news-ws")
private List<NewsWs> newsWs;
里面所用到的jar全部都是jdk自带的,无需第三方包。
执行结果:前半部是对象转XML,后半部是XML转对象。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<context>
<datasource>
<db-type>Mysql</db-type>
<url>http://127.0.0.1:8080/Hessian/hello</url>
<user>root</user>
<password>123</password>
</datasource>
<news-ws-define>
<news-ws>
<sql>sql1</sql>
<time>2013年7月13日</time>
<column>新闻内容1</column>
<ally-code>验证1</ally-code>
</news-ws>
<news-ws>
<sql>sql2</sql>
<time>2013年7月14日</time>
<column>新闻内容2</column>
<ally-code></ally-code>
</news-ws>
<news-ws>
<sql>sql3</sql>
<time>2013年7月15日</time>
<column>新闻内容3</column>
<ally-code>验证1</ally-code>
</news-ws>
</news-ws-define>
</context>
Datasource [dbtype=Mysql, url=http://127.0.0.1:8080/Hessian/hello, user=root, password=123]
**************
sql2
2013年7月14日
新闻内容2
**************
sql1
2013年7月13日
新闻内容1
验证1
**************
sql3
2013年7月15日
新闻内容3
转自:http://zhidao.baidu.com/link?url=w5LmqgYeksZItDa0AvY7ruZ_HPl9HYiybrcP-kgRkRTumpdjCEaRxxmtTBVZO93vbQ-tO37mC8NoE6LXl4rri_
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: