java使用dom4j解析xml文件

2019 精帖
0 1804

引入pom文件

<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.0.0</version>
</dependency>


需要解析的xml文件如下:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <TuitionViewResponse xmlns="http://www.szhtkj.com.cn/">
      <TuitionViewResult>
        <ReturnCode>00000</ReturnCode>
        <ReturnMsg>查询成功</ReturnMsg>
        <Xh>xxxxxxxxxxxx</Xh>
        <Xm>xxx</Xm>
        <Content>
          <Info>
            <Sfqjdm>2016</Sfqjdm>
            <Sfqjmc>2016</Sfqjmc>
            <Sfxmdm>012002</Sfxmdm>
          </Info>
          <Info>
            <Sfqjdm>2016</Sfqjdm>
            <Sfqjmc>2016</Sfqjmc>
            <Sfxmdm>013</Sfxmdm>
          </Info>
           </Content>
      </TuitionViewResult>
    </TuitionViewResponse>
  </soap:Body>
</soap:Envelope>


为了方便下方测试代码直接将xml内容用String储存

package com.dzqc.yx.dom;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Dom4jTest {
	public static void main(String[] args) {
		String s="<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n" + 
				"  <soap:Body>\r\n" + 
				"    <TuitionViewResponse xmlns=\"http://www.szhtkj.com.cn/\">\r\n" + 
				"      <TuitionViewResult>\r\n" + 
				"        <ReturnCode>00000</ReturnCode>\r\n" + 
				"        <ReturnMsg>查询成功</ReturnMsg>\r\n" + 
				"        <Xh>xxxxxxxxxxxx</Xh>\r\n" + 
				"        <Xm>xxx</Xm>\r\n" + 
				"        <Content>\r\n" + 
				"          <Info>\r\n" + 
				"            <Sfqjdm>2016</Sfqjdm>\r\n" + 
				"            <Sfqjmc>2016</Sfqjmc>\r\n" + 
				"            <Sfxmdm>012002</Sfxmdm>\r\n" + 
				"          </Info>\r\n" + 
				"          <Info>\r\n" + 
				"            <Sfqjdm>2016</Sfqjdm>\r\n" + 
				"            <Sfqjmc>2016</Sfqjmc>\r\n" + 
				"            <Sfxmdm>013</Sfxmdm>\r\n" + 
				"          </Info>\r\n" + 
				"           </Content>\r\n" + 
				"      </TuitionViewResult>\r\n" + 
				"    </TuitionViewResponse>\r\n" + 
				"  </soap:Body>\r\n" + 
				"</soap:Envelope>";
		getXmlAttribute(s);
	}
	public static void getXmlAttribute(String soap) {
		try {
			Document document = DocumentHelper.parseText(soap);
			Element envelope = document.getRootElement();
			System.out.println(envelope.getName());
			Element body=envelope.element("Body");
			System.out.println("\t"+body.getName());
			Element tuitionViewResponse=body.element("TuitionViewResponse");
			System.out.println("\t\t"+tuitionViewResponse.getName());
			Element tuitionViewResult=tuitionViewResponse.element("TuitionViewResult");
			System.out.println("\t\t\t"+tuitionViewResult.getName());
			Element returnCode=tuitionViewResult.element("ReturnCode");
			System.out.println("\t\t\t\t"+returnCode.getName()+" :<"+returnCode.getTextTrim()+">");
			Element returnMsg=tuitionViewResult.element("ReturnMsg");
			System.out.println("\t\t\t\t"+returnMsg.getName()+" :<"+returnMsg.getTextTrim()+">");
			Element xh=tuitionViewResult.element("Xh");
			System.out.println("\t\t\t\t"+xh.getName()+" :<"+xh.getTextTrim()+">");
			Element xm=tuitionViewResult.element("Xm");
			System.out.println("\t\t\t\t"+xm.getName()+" :<"+xm.getTextTrim()+">");
			
			Element content=tuitionViewResult.element("Content");
			List<Element> infos=content.elements("Info");
			for(Element info:infos) {
				System.out.println("\t\t\t\t\t"+info.getName());
				Element sfqjdm = info.element("Sfqjdm");
				System.out.println("\t\t\t\t\t\t"+sfqjdm.getName()+" :<"+sfqjdm.getTextTrim()+">");
				Element sfqjmc = info.element("Sfqjmc");
				System.out.println("\t\t\t\t\t\t"+sfqjmc.getName()+" :<"+sfqjmc.getTextTrim()+">");
				Element sfxmdm = info.element("Sfxmdm");
				System.out.println("\t\t\t\t\t\t"+sfxmdm.getName()+" :<"+sfxmdm.getTextTrim()+">");
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
    }
}


解析输出结果如下

Envelope
	Body
		TuitionViewResponse
			TuitionViewResult
				ReturnCode :<00000>
				ReturnMsg :<查询成功>
				Xh :<xxxxxxxxxxxx>
				Xm :<xxx>
					Info
						Sfqjdm :<2016>
						Sfqjmc :<2016>
						Sfxmdm :<012002>
					Info
						Sfqjdm :<2016>
						Sfqjmc :<2016>
						Sfxmdm :<013>


留言(0)
加载更多
猜你喜欢