首页 » Java » Java处理XML

Java处理XML

原文 http://blog.csdn.net/fighting_sxw/article/details/79249133

2018-02-04 02:00:26阅读(337)

dom4j和xstream下载

一、dom4j解析XML

package com.XML;
import Java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class DealXML {
    private String content="";
    private static String XMLStr="<xml>"
            + "<ToUserName><![CDATA[ToUserName]]></ToUserName>"
            + "<FromUserName><![CDATA[FromUserName]]></FromUserName>"
            + "<CreateTime>1517658190</CreateTime>"
            + "<MsgType><![CDATA[text]]></MsgType>"
            + "<Content><![CDATA[你好]]></Content>"
            + "<MsgId>6518292292999800190</MsgId>"
            + "</xml>";
    /**
     * 遍历XML
     * @param node
     * @return
     */
    public  String readNodes(Element node){
        content+=node.getName()+":"+node.getTextTrim()+"\n";
      //递归遍历当前节点所有的子节点  
        List<Element> listElement=node.elements();//所有一级子节点的list  
        for(Element e:listElement){//遍历所有一级子节点  
            readNodes(e);//递归  
        }
        return content;
    }
    /**
     * 根据节点名称,读取节点值
     * @param node
     * @param name
     * @return
     */
    public static String readNode(Element root,String name){
        Element e=root.element(name);
        String nodeString=e.getTextTrim();
        return nodeString;
    }
    public static void main(String[] args) {
        Document document;
        try {
            document = DocumentHelper.parseText(XMLStr);
            Element root=document.getRootElement();
            DealXML dXml=new DealXML();
            System.out.println(dXml.readNodes(root));
            System.out.println("MsgType:"+dXml.readNode(root,"MsgType"));
            System.out.println("Content:"+dXml.readNode(root,"Content"));
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

运行结果如下:

xml:
ToUserName:ToUserName
FromUserName:FromUserName
CreateTime:1517658190
MsgType:text
Content:你好
MsgId:6518292292999800190
MsgType:text
Content:你好

二、XStream实现对象序列化为XML

Article转换为只含有一级节点的XML

package com.XML;
public class Article {
    private String Title;
    private String Description;
    private String PicUrl;
    private String Url;
    public String getTitle() {
        return Title;
    }
    public void setTitle(String title) {
        Title = title;
    }
    public String getDescription() {
        return Description;
    }
    public void setDescription(String description) {
        Description = description;
    }
    public String getPicUrl() {
        return PicUrl;
    }
    public void setPicUrl(String picUrl) {
        PicUrl = picUrl;
    }
    public String getUrl() {
        return Url;
    }
    public void setUrl(String url) {
        Url = url;
    }
}

ReplyNewsMsg转换为含有二级节点的XML

package com.XML;
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.XStream;
public class ReplyNewsMsg {
    protected String ToUserName;
    protected String FromUserName;
    protected String CreateTime;
    protected String MsgType;
    private int ArticleCount;
    private List<Article> Articles;
    public String getToUserName() {
        return ToUserName;
    }
    public void setToUserName(String toUserName) {
        ToUserName = toUserName;
    }
    public String getFromUserName() {
        return FromUserName;
    }
    public void setFromUserName(String fromUserName) {
        FromUserName = fromUserName;
    }
    public String getCreateTime() {
        return CreateTime;
    }
    public void setCreateTime(String createTime) {
        CreateTime = createTime;
    }
    public String getMsgType() {
        return MsgType;
    }
    public void setMsgType(String msgType) {
        MsgType = msgType;
    }
    public int getArticleCount() {
        return ArticleCount;
    }
    public void setArticleCount(int articleCount) {
        ArticleCount = articleCount;
    }
    public List<Article> getArticles() {
        return Articles;
    }
    public void setArticles(List<Article> articles) {
        Articles = articles;
    }
    public static void main(String[] args) {
        //只有一级节点
        Article article=new Article();
        article.setTitle("title");
        article.setDescription("description");
        article.setPicUrl("picUrl");
        article.setUrl("url");
        XStream xstream=new XStream();
        xstream.alias("xml", article.getClass());
        System.out.println(xstream.toXML(article));
        //只有一级节点
        Article article2=new Article();
        article2.setTitle("title1");
        article2.setDescription("description1");
        article2.setPicUrl("picUrl1");
        article2.setUrl("url1");
        xstream.alias("xml", article2.getClass());
        System.out.println(xstream.toXML(article2));
        //含有二级节点
        List<Article> ls=new ArrayList<Article>();
        ls.add(article);
        ls.add(article2);
        ReplyNewsMsg replyNewsMsg=new ReplyNewsMsg();
        replyNewsMsg.setToUserName("toUserName");
        replyNewsMsg.setFromUserName("fromUserName");
        replyNewsMsg.setCreateTime("createTime");
        replyNewsMsg.setArticleCount(ls.size());
        replyNewsMsg.setArticles(ls);
        xstream.alias("xml", replyNewsMsg.getClass());
        //设置二级节点
        for(Article article3:ls){
            xstream.alias("item", article3.getClass());
        }
        System.out.println(xstream.toXML(replyNewsMsg));
    }
}

运行结果如下:

<xml>
  <Title>title</Title>
  <Description>description</Description>
  <PicUrl>picUrl</PicUrl>
  <Url>url</Url>
</xml>
<xml>
  <Title>title1</Title>
  <Description>description1</Description>
  <PicUrl>picUrl1</PicUrl>
  <Url>url1</Url>
</xml>
<xml>
  <ToUserName>toUserName</ToUserName>
  <FromUserName>fromUserName</FromUserName>
  <CreateTime>createTime</CreateTime>
  <ArticleCount>2</ArticleCount>
  <Articles>
    <item>
      <Title>title</Title>
      <Description>description</Description>
      <PicUrl>picUrl</PicUrl>
      <Url>url</Url>
    </item>
    <item>
      <Title>title1</Title>
      <Description>description1</Description>
      <PicUrl>picUrl1</PicUrl>
      <Url>url1</Url>
    </item>
  </Articles>
</xml>

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹