zoukankan      html  css  js  c++  java
  • XML格式化及解析

    XML格式化工具:notepad++   下载地址 http://rj.baidu.com/soft/detail/13478.html?ald    一个教程地址:http://jingyan.baidu.com/album/4b07be3c69172648b380f383.html  

    一个很好的notepad教程地址: http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htmls/npp_func_add_comment.html

    一个解析xml 成map的简单的工具类,只支持如下如下格式化的xml 

    1 <object>
    2     <栏目>李树鹏</栏目>
    3     <request_day>22</request_day>
    4     <day>22</day>
    5     <name>tttt</name>
    6 </object>

    XMLValueObject类的全部代码:

      1 import java.io.StringReader;
      2 import java.util.ArrayList;
      3 import java.util.LinkedHashMap;
      4 import java.util.List;
      5 import java.util.Map;
      6 import org.apache.commons.lang.StringUtils;
      7 import org.apache.log4j.Logger;
      8 import org.dom4j.Document;
      9 import org.dom4j.DocumentException;
     10 import org.dom4j.DocumentHelper;
     11 import org.dom4j.Element;
     12 import org.dom4j.io.SAXReader;
     13 
     14 public class XMLValueObject implements java.io.Serializable {
     15 
     16     private static final long serialVersionUID = -3163892375347045824L;
     17 
     18     private static Logger logger = Logger.getLogger(XMLValueObject.class);
     19 
     20     private Map<String, List<String>> map;
     21 
     22     public XMLValueObject() {
     23         map = new LinkedHashMap<String, List<String>>();
     24     }
     25 
     26     public void put(String key, String value) {
     27         if (value == null) {
     28             return;
     29         }
     30 
     31         List<String> list = map.get(key);
     32         if (list == null) {
     33             list = new ArrayList<String>();
     34             map.put(key, list);
     35         }
     36         list.removeAll(list);
     37         list.add(value);
     38     }
     39 
     40     public void put(String key, String[] values) {
     41         if (values == null || values.length == 0) {
     42             return;
     43         }
     44 
     45         List<String> list = map.get(key);
     46         if (list == null) {
     47             list = new ArrayList<String>();
     48             map.put(key, list);
     49         }
     50         for (String value : values) {
     51             list.add(value);
     52         }
     53     }
     54 
     55     public Object get(String key) {
     56         List<String> list = map.get(key);
     57         return list == null || list.isEmpty() ? null : list.get(0);
     58     }
     59 
     60     public Object[] getArray(String key) {
     61         List<String> list = map.get(key);
     62         return list == null ? new String[0] : list.toArray(new String[0]);
     63     }
     64 
     65     @SuppressWarnings("unchecked")
     66     public void fromXML(String xml) {
     67      try{
     68          if (StringUtils.isEmpty(xml)) {
     69              return;
     70          }
     71         StringReader in = new StringReader(xml);
     72         SAXReader reader = new SAXReader(false);
     73         Document doc = null;
     74         try {
     75             doc = reader.read(in);
     76         }
     77         catch (DocumentException e) {
     78             logger.error("Could not read XML[" + xml + "]", e);
     79         }
     80         finally {
     81             in.close();
     82         }
     83 
     84         Element root = doc.getRootElement();
     85         List<Element> children = (List<Element>) root.elements();
     86         for (Element child : children) {
     87             put(child.getName(), child.getText());
     88         } 
     89         
     90      }catch(Exception e)
     91      {
     92          //System.out.print("错误"+e.getMessage()+":"+xml);
     93          e.printStackTrace();
     94      }
     95         
     96     }
     97 
     98     public String toXML() {
     99         Document doc = DocumentHelper.createDocument();
    100         Element root = doc.addElement("object");
    101         //root.addAttribute("class", XMLValueObject.class.getName());
    102 
    103         for (Map.Entry<String, List<String>> entry : map.entrySet()) {
    104             String key = entry.getKey();
    105             List<String> list = entry.getValue();
    106 
    107             for (String value : list) {
    108                 Element child = root.addElement(key);
    109                 child.addText(value);
    110             }
    111         }
    112 
    113         return doc.asXML();
    114     }
    115     
    116     public Map<String, List<String>> getMap() {
    117         return map;
    118     }
    119 
    120     public void setMap(Map<String, List<String>> map) {
    121         this.map = map;
    122     }
    123 }

    测试代码:

    1 public static void main(String[] args) {
    2         XMLValueObject vo = new XMLValueObject();
    3         vo.fromXML("<object><栏目>李树鹏</栏目><request_day>22</request_day><day>22</day><name>tttt</name></object>");
    4         System.out.println(vo.get("栏目"));
    5         vo.put("temp", "1212");
    6         System.out.println(vo.toXML());
    7     }
  • 相关阅读:
    java int转byte数组
    业务网关-操作手册
    业务网关-配置
    H2数据库的导入、导出(CSV)
    sequelize踩坑处理:时间格式化
    引入koa-static处理静态资源
    MySql利用case when实现批量更新多条记录的不同值实现方法
    Sequelize增删改查及批量新增或批量更新(bulkCreate的updateOnDuplicate)等常用接口
    数据结构队列、双端队列、队列系算法题解析
    配置webpack中externals来减少打包后vendor.js的体积
  • 原文地址:https://www.cnblogs.com/lishupeng/p/5494362.html
Copyright © 2011-2022 走看看