java dom4j解析xml文件
原创    stackover    发表于:2017-09-14 11:11:59
  阅读 :131   收藏   编辑

摘要:dom4j解析指定模板的xml文件!

一:首先建立一个java项目

二:导入dom4j的jar,我这里导入的是dom4j-1.6.1.jar,具体请自行百度下载

三:xml模板内容,我这里是直接解析xml文件,也可以把xml文件转换为xml字符串进行解析:

<?xml version="1.0" encoding="UTF-8"?>  
        <message>  
            <body>  
                <field-list name="RECORD" size="2">  
                    <field-list name="1">  
                        <field name="messageId">1_20170116175008968_102157946</field>  
                        <field name="mobilePhone">18611966723</field>  
                        <field name="resultCode">-10</field>  
                    </field-list>  
                    <field-list name="2">  
                        <field name="messageId">1_20170116175008968_102157946</field>  
                        <field name="mobilePhone">13521196327</field>  
                        <field name="resultCode">-10</field>  
                    </field-list>  
                </field-list>  
                <field name="resultCode">0</field>  
                <field name="errorCode">0</field>  
            </body>  
        </message>

四:java代码:

package com.wwtl.sms;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlTest {

    public static void main(String[] args) {
        //1.读取XML文件,获得document对象              
        SAXReader reader = new SAXReader();               
        try {
            Document document = reader.read(new File("E:\\smsworkspace\\WwtlSmsDemo\\src\\text.xml"));
            Element rootElement = document.getRootElement();//获取根节点
            System.out.println("根节点的名称:" + rootElement.getName());
            Iterator iter = rootElement.elementIterator("body");//获取跟节点下的子节点body
            //遍历body节点
            while(iter.hasNext()){
                Element recordEle = (Element) iter.next();
                Iterator elementIterator = recordEle.elementIterator("field-list");
                while(elementIterator.hasNext()){
                    Element element = (Element) elementIterator.next();
                    Iterator elementIterator2 = element.elementIterator();
                    while(elementIterator2.hasNext()){
                        Element element2 = (Element)elementIterator2.next();
                        Iterator elementIterator3 = element2.elementIterator("field");
                        while(elementIterator3.hasNext()){
                            Element element3 = (Element)elementIterator3.next();
                            List<Attribute> attributes = element3.attributes();
                            for (Attribute attribute : attributes) {
                                //String name = attribute.getName();
                                String value = attribute.getValue();
                                //System.out.println("当前节点名称:"+element3.getName() + " 当前节点属性名称:"+name+" 当前节点属性值:"+value + " 当前节点的内容:"+element3.getTextTrim());
                                if(value.equals("resultCode")){
                                    System.out.println("节点的内容:" + element3.getTextTrim());
                                    if(element3.getTextTrim().equals("0")){
                                        System.out.println("短信发送success!");
                                    }else{
                                        System.out.println("短信发送fail!");
                                    }
                                }
                            }
                        }

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }  
    }

}

转载:http://blog.csdn.net/sxdtzhaoxinguo/article/details/54578169

评论
条评论