Java对下面XML文档解析要求封装为两个类
<?xmlversion="1.0"encoding="ISO-8859-1"?><context><datasource><db-type>Mysql</db-type...
<?xml version="1.0" encoding="ISO-8859-1" ?>
<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>..................... </datasource>封装到类
public class DataSource {
public String dbtype;
public String url;
public String user;
public String password;}
将 <news-ws-define>.............................. </news-ws-define>封装到类
public class NewsDefine {
public String sql;
public String time;
public String column;
public String allycode;}
最后再把每 一个 <news-ws>.......</news-ws> 放到hashmap 中 在用list包起来
谢谢了 急求大神帮助
解析的方法从saxreader 或者 dom中选择一种吧 谢谢拉 展开
<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>..................... </datasource>封装到类
public class DataSource {
public String dbtype;
public String url;
public String user;
public String password;}
将 <news-ws-define>.............................. </news-ws-define>封装到类
public class NewsDefine {
public String sql;
public String time;
public String column;
public String allycode;}
最后再把每 一个 <news-ws>.......</news-ws> 放到hashmap 中 在用list包起来
谢谢了 急求大神帮助
解析的方法从saxreader 或者 dom中选择一种吧 谢谢拉 展开
1个回答
展开全部
建议你用JAXB,简单实用。我根据你的要求写了个例子,应该满足你的要求。
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={"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
验证1
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={"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
验证1
更多追问追答
追问
非常感谢啊 不过我这个已经做好了
你知道Quartz 吗 加入那个 time 里面放的是cron 表达式 我要让Quartz在执行时是执行由于它对应的sql语句 有什么好的方法的哦 list 包了map map下为一条新闻
追答
Quartz了解一些,spring下面的一个定时器。
不太明白你要定时操作什么样的业务。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询