XML解析的例子

来源:互联网 发布:淘宝明星zero距离签名 编辑:程序博客网 时间:2024/05/22 00:35

xml数据格式

xml ="<?xml version='1.0' encoding='utf-8'?><resultInfo><result><xh>222222</xh><jfzt>未缴费</jfzt></result><result>

<xh>111111</xh><jfzt>缴费</jfzt></result></resultInfo>" ;

解析上一条字符串数据

</pre></p><p><span style="white-space: pre;"></span><pre name="code" class="java"><pre name="code" class="java" style="font-size: 14px;"><span style="background-color: rgb(153, 255, 153);"></span><pre name="code" class="java">
<span style="font-size:14px;color:#ff6600;background-color: rgb(255, 204, 153);"><strong><span style="margin: 0px; padding: 0px; border: none; background-color: rgb(248, 248, 248); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;"></span></strong></span>
<span style="font-size:14px;color:#ff6600;background-color: rgb(255, 204, 153);"><span style="font-weight: bold;"><span style="margin: 0px; padding: 0px; border: none; background-color: rgb(248, 248, 248); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;"><span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);"><span style="font-size: 14px; background-color: rgb(102, 255, 255);">String xml = </span></span></span></span></span>

xml ="<?xml version='1.0' encoding='utf-8'?><resultInfo><result><xh>222222</xh><jfzt>未缴费</jfzt></result><result>

<xh>111111</xh><jfzt>缴费</jfzt></result></resultInfo>" ;

<span style="font-size:14px;color:#ff6600;background-color: rgb(255, 204, 153);"><span style="font-weight: bold;"><span style="margin: 0px; padding: 0px; border: none; background-color: rgb(248, 248, 248); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;"><span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);"><span style="font-size: 14px; background-color: rgb(102, 255, 255);"></span></span></span></span></span><pre name="code" class="java" style="font-size: 14px;"><span style="background-color: rgb(153, 255, 153);"></span><pre name="code" class="java"><span style="color: rgb(255, 102, 0); background-color: rgb(255, 204, 153);"><strong>//此处若是文件,则可以用<span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">SAXReader saxReader = </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: rgb(248, 248, 248); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: rgb(248, 248, 248); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;"> SAXReader(<span style="color: rgb(0, 0, 255);">"C:/s.xml"</span>); </span></strong></span>
<span style="color: rgb(255, 102, 0); background-color: rgb(255, 204, 153);"><strong><span style="margin: 0px; padding: 0px; border: none; background-color: rgb(248, 248, 248); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">//Document document = saxReader.read(fis); 也可以获得dom</span></strong></span>

<span style="background-color: rgb(153, 255, 153);">org.dom4j.Document document;</span>
<span style="font-size:14px;color:#ff6600;background-color: rgb(255, 204, 153);"><strong>//获得dom</strong></span><span style="font-size: 14px; background-color: rgb(153, 255, 153);">document = DocumentHelper.parseText(xml);</span>
<span style="font-size:14px;"><strong style="background-color: rgb(255, 204, 204);">//得到根节点</strong></span><span style="background-color: rgb(153, 255, 153); font-size: 14px;">Element rootElement = document.getRootElement();</span>
<span style="color:#ffcccc;background-color: rgb(255, 0, 0);"><strong>//得到所有result元素的集合</strong></span><span style="background-color: rgb(153, 255, 153);">List<Element> list = rootElement.elements("result");</span>
<pre name="code" class="java" style="font-size: 14px;"><span style="background-color: rgb(255, 204, 204);"><strong>//数据库相关联的类</strong></span><span style="font-weight: bold; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 204, 204);">WebXsjfztData</span>
List<WebXsjfztData> webXsjfztDataList = new ArrayList<WebXsjfztData>();for(Element element : list){
<span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(255, 204, 204);"><strong>//获得每个result元素下的素有元素</strong></span><span style="background-color: rgb(153, 255, 153);">     </span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">List<Element> list2 = element.elements();</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">WebXsjfztData webXsjfztData = new WebXsjfztData();</span>
<span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><strong style="background-color: rgb(255, 204, 204);">//遍历<span style="font-family: Arial, Helvetica, sans-serif;">result元素下的素有元素</span></strong><span style="background-color: rgb(153, 255, 153);"></span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">for(Element element2 : list2){</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">String columnName = element2.getName();</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">String columnValue = element2.getText();</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">if("xh".equals(columnName)){</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">webXsjfztData.setXh(columnValue);</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">}else{</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">webXsjfztData.setJfzt(columnValue);</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">}</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">}</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">webXsjfztDataList.add(webXsjfztData);}</span><span style="background-color: rgb(255, 204, 204);"><strong>//将缴费状态数据保存到数据库中</strong></span><span style="background-color: rgb(153, 255, 153);">GetWebServiceDao getWebServiceDao = new GetWebServiceDao();for(WebXsjfztData webXsjfztData : webXsjfztDataList){</span><span style="background-color: rgb(153, 255, 153); white-space: pre;"></span><span style="background-color: rgb(153, 255, 153);">getWebServiceDao.saveWebXsjfzt(webXsjfztData);}</span>








0 0
原创粉丝点击