实例一:获取指定两个标签之间的数据

XML数据格式:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetAlarmTypeResponse xmlns="YKITSJLW">
            <GetAlarmTypeResult>报警,断电,调校,超量程,分站故障,不巡检,暂停,传感器故障</GetAlarmTypeResult>
        </GetAlarmTypeResponse>
    </soap:Body>
</soap:Envelope>

目的:获取<GetAlarmTypeResult>和</GetAlarmTypeResult> 之间的数据

方法一:

package yk.bigdata.dl.ias.fileParse;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import yk.bigdata.dl.ias.bean.BeanAlarmType;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: cjj
 * @Date: Created in 11:09 2019/7/9
 * @Description: 
 */
public class AlarmTypeXmlFileParse {
    public static dom4jXml(String interfaceData) throws Exception{
        List<Element> elementList = null;
        
        SAXReader sr = new SAXReader();
        Document document = sr.read(new ByteArrayInputStream(interfaceData.getBytes())); // 传入需要解析的数据
        Element root = document.getRootElement();
        elementList = root.elements();
for (Element e : elementList) {
            // 解析标签下一级标签
            Element e1 = e.element("GetAlarmTypeResponse");
            List<Element> elementList1 = e1.elements();
            for (Element el:elementList1){
                System.out.println(e1.elementText("GetAlarmTypeResult"));            
            }
        }
    }
}

方法二:

      // 获取指定标签的index
        int indexbegin = xmlStr.indexOf("<GetAlarmTypeResult>");
        int indexend = xmlStr.indexOf("</GetAlarmTypeResult>");
        System.out.println("<TrmSeqNum>的位置===" + indexbegin);
        System.out.println("</TrmSeqNum>的位置===" + indexend);
        // 截取指定index之间的数据
        String jsonStr = xmlStr.substring(indexbegin + 25, indexend); 
     System.out.println(jsonStr);
实例二:获取指定标签下的所有子标签的值

XML数据格式:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetTagValueJsonResponse xmlns="YKITSJLW">
            <GetTagValueJsonResult>
                <Table diffgr:id="Table1" msdata:rowOrder="0">
                    <AlarmState>调校</AlarmState>
                    <TagKey>YK03112034112</TagKey>
                    <TagName>1611运顺回风流CO</TagName>
                    <Value>0</Value>
                    <AlarmTime>2019-06-01T13:44:35.477+08:00</AlarmTime>
                    <AlarmRtnTime>2019-06-02T16:48:05.333+08:00</AlarmRtnTime>
                    <TimeInState>97410</TimeInState>
                    <SysGroup>安全监测监控系统</SysGroup>
                    <TagType>一氧化碳</TagType>
                    <EngUnit>ppm</EngUnit>
                    <DataType>1</DataType>
                    <HiAlarmLimit>24</HiAlarmLimit>
                    <LoAlarmLimit>0</LoAlarmLimit>
                    <UnitName>小屯煤矿</UnitName>
                </Table>

                <Table diffgr:id="Table2" msdata:rowOrder="1">
                    <AlarmState>调校</AlarmState>
                    <TagKey>YK03113022114</TagKey>
                    <TagName>1811运顺管道红外甲烷</TagName>
                    <Value>2.912</Value>
                    <AlarmTime>2019-06-01T01:39:36.41+08:00</AlarmTime>
                    <AlarmRtnTime>2019-06-01T03:07:36.377+08:00</AlarmRtnTime>
                    <TimeInState>5280</TimeInState>
                    <SysGroup>安全监测监控系统</SysGroup>
                    <TagType>高浓度甲烷</TagType>
                    <EngUnit>%CH4</EngUnit>
                    <DataType>1</DataType>
                    <HiAlarmLimit>0</HiAlarmLimit>
                    <LoAlarmLimit>0</LoAlarmLimit>
                    <UnitName>五轮山煤矿</UnitName>
                </Table>
           </GetTagValueJsonResult>
        </GetTagValueJsonResponse>
    </soap:Body>
</soap:Envelope>

目的:获取<Table>标签下的所有子标签后面跟的值,创建一个对象,用对象来接收取出来的值。

bean代码:

package yk.bigdata.dl.ias.bean;

import static yk.bigdata.dl.ias.getData.GetTime.getNowTime;
/**
 * Created with IntelliJ IDEA.
 * User: 吹静静
 * Date: 2019/4/7
 * Time: 9:31
 * Description: bean
 */
/**
    *  <AlarmState>调校</AlarmState>
    *  <TagKey>YK03112034112</TagKey>
    *  <TagName>1611运顺回风流CO</TagName>
    *  <Value>0</Value>
    *  <AlarmTime>2019-06-01T13:44:35.477+08:00</AlarmTime>
    *  <AlarmRtnTime>2019-06-02T16:48:05.333+08:00</AlarmRtnTime>
    *  <TimeInState>97410</TimeInState>
    *  <SysGroup>安全监测监控系统</SysGroup>
    *  <TagType>一氧化碳</TagType>
    *  <EngUnit>ppm</EngUnit>
    *  <DataType>1</DataType>
    *  <HiAlarmLimit>24</HiAlarmLimit>
    *  <LoAlarmLimit>0</LoAlarmLimit>
    *  <UnitName>小屯煤矿</UnitName>
 */
public class BeanAlarmHis {

    private String AlarmState;
    private String TagKey;
    private String TagName;
    private String Value;
    private String AlarmTime;
    private String AlarmRtnTime;
    private String TimeInState;
    private String SysGroup;
    private String TagType;
    private String EngUnit;
    private String DataType;
    private String HiAlarmLimit;
    private String LoAlarmLimit;
    private String UnitName;
    private String KBN0 = "0"; // 流式处理的标志
    private String KBN1 = "1"; // 离线处理的标志
    private String CREATE_TIME;

    public String getAlarmState() {
        return AlarmState;
    }

    public void setAlarmState(String alarmState) {
        AlarmState = alarmState;
    }

    public String getTagKey() {
        return TagKey;
    }

    public void setTagKey(String tagKey) {
        TagKey = tagKey;
    }

    public String getTagName() {
        return TagName;
    }

    public void setTagName(String tagName) {
        TagName = tagName;
    }

    public String getValue() {
        return Value;
    }

    public void setValue(String value) {
        Value = value;
    }

    public String getAlarmTime() {
        return AlarmTime;
    }

    public void setAlarmTime(String alarmTime) {
        AlarmTime = alarmTime;
    }

    public String getAlarmRtnTime() {
        return AlarmRtnTime;
    }

    public void setAlarmRtnTime(String alarmRtnTime) {
        AlarmRtnTime = alarmRtnTime;
    }

    public String getTimeInState() {
        return TimeInState;
    }

    public void setTimeInState(String timeInState) {
        TimeInState = timeInState;
    }

    public String getSysGroup() {
        return SysGroup;
    }

    public void setSysGroup(String sysGroup) {
        SysGroup = sysGroup;
    }

    public String getTagType() {
        return TagType;
    }

    public void setTagType(String tagType) {
        TagType = tagType;
    }

    public String getEngUnit() {
        return EngUnit;
    }

    public void setEngUnit(String engUnit) {
        EngUnit = engUnit;
    }

    public String getDataType() {
        return DataType;
    }

    public void setDataType(String dataType) {
        DataType = dataType;
    }

    public String getHiAlarmLimit() {
        return HiAlarmLimit;
    }

    public void setHiAlarmLimit(String hiAlarmLimit) {
        HiAlarmLimit = hiAlarmLimit;
    }

    public String getLoAlarmLimit() {
        return LoAlarmLimit;
    }

    public void setLoAlarmLimit(String loAlarmLimit) {
        LoAlarmLimit = loAlarmLimit;
    }

    public String getUnitName() {
        return UnitName;
    }

    public void setUnitName(String unitName) {
        UnitName = unitName;
    }

    public String getKBN0() {
        KBN0 = "0";
        return KBN0;
    }

    public String getKBN1() {
        KBN1 = "1";
        return KBN1;
    }

    public String getCREATE_TIME() {
        CREATE_TIME = getNowTime();
        return CREATE_TIME;
    }

    @Override
    public String toString() {

        return "BeanAlarmHis{" +
                "AlarmState='" + AlarmState + '\'' +
                ", TagKey='" + TagKey + '\'' +
                ", TagName='" + TagName + '\'' +
                ", Value='" + Value + '\'' +
                ", AlarmTime='" + AlarmTime + '\'' +
                ", AlarmRtnTime='" + AlarmRtnTime + '\'' +
                ", TimeInState='" + TimeInState + '\'' +
                ", SysGroup='" + SysGroup + '\'' +
                ", TagType='" + TagType + '\'' +
                ", EngUnit='" + EngUnit + '\'' +
                ", DataType='" + DataType + '\'' +
                ", HiAlarmLimit='" + HiAlarmLimit + '\'' +
                ", LoAlarmLimit='" + LoAlarmLimit + '\'' +
                ", UnitName='" + UnitName + '\'' +
                '}';
    }
}

解析代码:

package yk.bigdata.dl.ias.fileParse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import yk.bigdata.dl.ias.bean.BeanAlarmHis;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import static yk.bigdata.dl.ias.getData.GetTime.timeParse;
/**
 * Created with IntelliJ IDEA.
 * User: 吹静静
 * Date: 2019/7/3
 * Time: 9:31
 * Description: 解析GetAlarmHis数据
 */
public class AlarmHisXmlFileParse {

    public static List<BeanAlarmHis> dom4jXml(String interfaceData) throws Exception{
        List<Element> elementList = null;
        List<BeanAlarmHis> beanAlarmHisList = null;

        try {
            SAXReader sr = new SAXReader();
            Document document = sr.read(new ByteArrayInputStream(interfaceData.getBytes()));

            Element root = document.getRootElement();
            elementList = root.elements();
            beanAlarmHisList = new ArrayList();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        for (Element e : elementList) {
            // 解析标签下一级标签
            Element e1 = e.element("GetTagValueJsonResponse");
            Element e2 = e1.element("GetTagValueJsonResult");
            List<Element> elementList1 = e2.elements();
            for (Element el:elementList1){
                BeanAlarmHis beanAlarmHis = new BeanAlarmHis();
                beanAlarmHis.setAlarmState(el.elementText("AlarmState"));
                beanAlarmHis.setTagKey(el.elementText("TagKey"));
                beanAlarmHis.setTagName(el.elementText("TagName"));
                beanAlarmHis.setValue(el.elementText("Value"));
                beanAlarmHis.setAlarmTime(timeParse(el.elementText("AlarmTime")));  // 对时间格式进行转换
                beanAlarmHis.setAlarmRtnTime(timeParse(el.elementText("AlarmRtnTime")));  // 对时间格式进行转换
                beanAlarmHis.setTimeInState(el.elementText("TimeInState"));
                beanAlarmHis.setSysGroup(el.elementText("SysGroup"));
                beanAlarmHis.setTagType(el.elementText("TagType"));
                beanAlarmHis.setEngUnit(el.elementText("EngUnit"));
                beanAlarmHis.setDataType(el.elementText("DataType"));
                beanAlarmHis.setHiAlarmLimit(el.elementText("HiAlarmLimit"));
                beanAlarmHis.setLoAlarmLimit(el.elementText("LoAlarmLimit"));
                beanAlarmHis.setUnitName(el.elementText("UnitName"));
                beanAlarmHisList.add(beanAlarmHis);
            }
        }
        return beanAlarmHisList;
    }
}