• java转成xml


      1 package com.chinamcloud.cms.article.util;
      2 
      3 import com.chinamcloud.cms.article.dto.ZjyArticleDto;
      4 import com.google.common.collect.Maps;
      5 import org.assertj.core.util.Lists;
      6 import org.dom4j.Document;
      7 import org.dom4j.DocumentException;
      8 import org.dom4j.DocumentHelper;
      9 import org.dom4j.Element;
     10 
     11 import java.util.Iterator;
     12 import java.util.List;
     13 import java.util.Map;
     14 
     18 public class XmlUtil {
     19 
     20     public static void readStringXml(String xml) {
     21         Document doc = null;
     22         try {
     23             doc = DocumentHelper.parseText(xml); // 将字符串转为XML
     24             Element rootElt = doc.getRootElement(); // 获取根节点
     25             System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称
     26             Iterator iter = rootElt.elementIterator("head"); // 获取根节点下的子节点head
     27             // 遍历head节点
     28             while (iter.hasNext()) {
     29                 Element recordEle = (Element) iter.next();
     30                 String title = recordEle.elementTextTrim("title"); // 拿到head节点下的子节点title值
     31                 System.out.println("title:" + title);
     32                 Iterator iters = recordEle.elementIterator("script"); // 获取子节点head下的子节点script
     33                 // 遍历Header节点下的Response节点
     34                 while (iters.hasNext()) {
     35                     Element itemEle = (Element) iters.next();
     36                     String username = itemEle.elementTextTrim("username"); // 拿到head下的子节点script下的字节点username的值
     37                     String password = itemEle.elementTextTrim("password");
     38                     System.out.println("username:" + username);
     39                     System.out.println("password:" + password);
     40                 }
     41             }
     42             Iterator iterss = rootElt.elementIterator("body"); ///获取根节点下的子节点body
     43             // 遍历body节点
     44             while (iterss.hasNext()) {
     45                 Element recordEless = (Element) iterss.next();
     46                 String result = recordEless.elementTextTrim("result"); // 拿到body节点下的子节点result值
     47                 System.out.println("result:" + result);
     48                 Iterator itersElIterator = recordEless.elementIterator("form"); // 获取子节点body下的子节点form
     49                 // 遍历Header节点下的Response节点
     50                 while (itersElIterator.hasNext()) {
     51                     Element itemEle = (Element) itersElIterator.next();
     52                     String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子节点form下的字节点banlce的值
     53                     String subID = itemEle.elementTextTrim("subID");
     54                     System.out.println("banlce:" + banlce);
     55                     System.out.println("subID:" + subID);
     56                 }
     57             }
     58         } catch (DocumentException e) {
     59             e.printStackTrace();
     60         } catch (Exception e) {
     61             e.printStackTrace();
     62         }
     63     }
     64 
     65     public static Map<String,Object> readArticleXmlByZJY(String xml) {
     66         Document doc = null;
     67         Map<String,Object> map = Maps.newHashMap();
     68         List<ZjyArticleDto> zjyArticleDtoList = Lists.newArrayList();
     69         Integer totalPage = 0;
     70         Integer totalNum = 0;
     71         try {
     72             doc = DocumentHelper.parseText(xml); // 将字符串转为XML
     73             Element rootElt = doc.getRootElement(); // 获取根节点
     74             Iterator channel = rootElt.elementIterator("channel"); // 获取根节点下的子节点channel
     75             while (channel.hasNext()){
     76                 Element channelEle = (Element) channel.next();
     77                 totalPage = Integer.valueOf(channelEle.elementTextTrim("totalPage"));
     78                 totalNum = Integer.valueOf(channelEle.elementTextTrim("totalNum"));
     79                 Iterator item = channelEle.elementIterator("item"); // 获取channel节点下的子节点item
     80                 while (item.hasNext()){
     81                     Element itemEle = (Element) item.next();
     82                     String pk = itemEle.elementTextTrim("pk");
     83                     String title = itemEle.elementTextTrim("title");
     84                     String status = itemEle.elementTextTrim("status");
     85                     String region = itemEle.elementTextTrim("region");
     86                     String media = itemEle.elementTextTrim("media");
     87                     String coverUrl = itemEle.elementTextTrim("coverUrl");
     88                     String author = itemEle.elementTextTrim("author");
     89                     String ch = itemEle.elementTextTrim("ch");
     90                     String desc = itemEle.elementTextTrim("desc");
     91                     String content = itemEle.elementTextTrim("content");
     92                     String pubDate = itemEle.elementTextTrim("pubDate");
     93                     String type = itemEle.elementTextTrim("type");
     94                     String videoUrl = itemEle.elementTextTrim("videoUrl");
     95                     ZjyArticleDto zjyArticleDto = new ZjyArticleDto();
     96                     zjyArticleDto.setPk(pk);
     97                     zjyArticleDto.setAuthor(author);
     98                     zjyArticleDto.setCh(ch);
     99                     zjyArticleDto.setContent(content);
    100                     zjyArticleDto.setCoverUrl(coverUrl);
    101                     zjyArticleDto.setDesc(desc);
    102                     zjyArticleDto.setMedia(media);
    103                     zjyArticleDto.setPubDate(pubDate);
    104                     zjyArticleDto.setRegion(region);
    105                     zjyArticleDto.setStatus(status);
    106                     zjyArticleDto.setTitle(title);
    107                     zjyArticleDto.setType(type);
    108                     zjyArticleDto.setVideoUrl(videoUrl);
    109                     zjyArticleDtoList.add(zjyArticleDto);
    110                 }
    111             }
    112         } catch (DocumentException e) {
    113             e.printStackTrace();
    114         } catch (Exception e) {
    115             e.printStackTrace();
    116         }
    117         map.put("totalPage",totalPage);
    118         map.put("totalNum",totalNum);
    119         map.put("zjyArticleDtoList",zjyArticleDtoList);
    120         return map;
    121     }
    122 }
  • 相关阅读:
    Nginx 413 Request Entity Too Large
    tp U函数 logs
    div + css 边框 虚线
    html+css判断各个IE浏览器版本
    win7 cmd 常用命令
    mysql 常用命令
    ThinkPHP学习(二)理清ThinkPHP的目录结构及访问规则,创建第一个控制器
    将字符串 由一个字符集 转成 另一个字符集 及 随机生成中文
    Sublime Text 3 中实现编译C语言程序
    C语言入门
  • 原文地址:https://www.cnblogs.com/fenghh/p/12850756.html
Copyright © 2020-2023  润新知