zoukankan      html  css  js  c++  java
  • XML

    package com.founder.ec.common.utils;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    
    /**
     * xml工具类
     * @author miklchen
     *
     */
    public class XMLUtil {
    
        /**
         * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
         * @param strxml
         * @return
         * @throws JDOMException
         * @throws IOException
         */
        public static Map doXMLParse(String strxml) throws JDOMException, IOException {
            if(null == strxml || "".equals(strxml)) {
                return null;
            }
            
            Map m = new HashMap();
            InputStream in = HttpClientUtil.String2Inputstream(strxml);
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(in);
            Element root = doc.getRootElement();
            List list = root.getChildren();
            Iterator it = list.iterator();
            while(it.hasNext()) {
                Element e = (Element) it.next();
                String k = e.getName();
                String v = "";
                List children = e.getChildren();
                if(children.isEmpty()) {
                    v = e.getTextNormalize();
                } else {
                    v = XMLUtil.getChildrenText(children);
                }
                
                m.put(k, v);
            }
            
            //关闭流
            in.close();
            
            return m;
        }
        
        /**
         * 获取子结点的xml
         * @param children
         * @return String
         */
        public static String getChildrenText(List children) {
            StringBuffer sb = new StringBuffer();
            if(!children.isEmpty()) {
                Iterator it = children.iterator();
                while(it.hasNext()) {
                    Element e = (Element) it.next();
                    String name = e.getName();
                    String value = e.getTextNormalize();
                    List list = e.getChildren();
                    sb.append("<" + name + ">");
                    if(!list.isEmpty()) {
                        sb.append(XMLUtil.getChildrenText(list));
                    }
                    sb.append(value);
                    sb.append("</" + name + ">");
                }
            }
            
            return sb.toString();
        }
        
        /**
         * 获取xml编码字符集
         * @param strxml
         * @return
         * @throws IOException 
         * @throws JDOMException 
         */
        public static String getXMLEncoding(String strxml) throws JDOMException, IOException {
            InputStream in = HttpClientUtil.String2Inputstream(strxml);
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(in);
            in.close();
            return (String)doc.getProperty("encoding");
        }
        
        
    }
  • 相关阅读:
    Netty4学习笔记
    Essential Netty in Action 《Netty 实战(精髓)》
    【转】netty案例
    【转】Delphi TClientDataSet的使用
    Macbook brew 安装软件 一直updating Homebrew
    PHP的gc_collect_cycles函数
    Android开发手记之android.content.res.Resources$NotFoundException: Resource ID #0x7f04005e
    Android开发手记之duplicate entry: res/drawable/a.xml
    Android开发手记之Native Crash
    Java与C、C++的10大区别-总结
  • 原文地址:https://www.cnblogs.com/wangchuanfu/p/6140984.html
Copyright © 2011-2022 走看看