• 常见XML解析器


    xpp3

    官网

    http://www.extreme.indiana.edu/xgws/xsoap/xpp/

    简介

    Xml Pull Parser (in short XPP) is a streaming pull XML parser and should be used when there is a need to process quickly and efficiently all input elements (for example in SOAP processors).

    sax

    官网

    http://www.saxproject.org/

    简介

    SAX is the Simple API for XML, originally a Java-only API. SAX was the first widely adopted API for XML in Java, and is a “de facto” standard. The current version is SAX 2.0.1, and there are versions for several programming language environments other than Java.

    示例:解析微信post消息

    当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。

    <xml>
     <ToUserName><![CDATA[toUser]]></ToUserName>
     <FromUserName><![CDATA[fromUser]]></FromUserName> 
     <CreateTime>1348831860</CreateTime>
     <MsgType><![CDATA[text]]></MsgType>
     <Content><![CDATA[this is a test]]></Content>
     <MsgId>1234567890123456</MsgId>
    </xml

    以下是java代码片段:

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.servlet.ServletInputStream;
    import javax.servlet.http.HttpServletRequest;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    /**
     * 消息工具类
     * 
     */
    public class WeixinUtils {
    
        /**
         * 解析微信发来的请求(XML)
         * 
         * @param request
         * @return
         * @throws IOException
         */
        @SuppressWarnings("unchecked")
        public static Map<String, String> parseXml(HttpServletRequest request) throws IOException {
            ServletInputStream inputStream = request.getInputStream();
            SAXReader reader = new SAXReader();
            Map<String, String> resultMap = null;
            try {
                // 这个 doc 不会是 null
                Document document = reader.read(inputStream);
                Element rootElement = document.getRootElement();
                List<Element> eList = rootElement.elements();
                resultMap = new HashMap<String, String>();
                for (Element e : eList) {
                    resultMap.put(e.getName(), e.getText());
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null) inputStream.close();
            }
            return resultMap;
        }
    }

    ps:

    想知道 void java.io.InputStream.close() throws IOException 怎么用吗?

    Ctrl+Shift+G 看看别人怎么用的吧

  • 相关阅读:
    蛙蛙推荐:改进了一个DBAccess类,顺便说说啥是线程安全
    蛙蛙推荐:SQLServer优化资料整理
    快速配置Eclipse3.1.1+Tomcat5.0+Lomboz dodo
    建立asp.net应用程序提示:无法与服务器建立连接 dodo
    tomcat连接sqlserver配置 dodo
    如何让DNN页面中的html控件中的文本项显示为中文? dodo
    DropDownList 不能有多个项被选定 dodo
    转换DATAREADER为 dataset dodo
    使用ComponentArt.WebUI.for.Asp.net.3.0的TreeView控件 dodo
    ASP.NET的WEB项目不能添加WEB用户控件的解决 dodo
  • 原文地址:https://www.cnblogs.com/zno2/p/5688621.html
Copyright © 2020-2023  润新知