JAVA-DOM4J-获取XML字段内容笔记练习

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

import java.util.Iterator;


public class XMLDemo {
    public static void main(String args[]){

        String xmlStr = "";

        xmlStr += " <payload>";
        xmlStr += "  <param key=\"data\" type=\"XML\" >";
        xmlStr += "   <![CDATA[    ";
        xmlStr += "   <Request>";
        xmlStr += "     <RequestContent>";
        xmlStr += "     <Document>";
        xmlStr += "     <RecordSet id=\"1\">";
        xmlStr += "        <Master name=\"xmgcuc_t\" node_id=\"1\">";
        xmlStr += "         <Record>";
        xmlStr += "          <Field name=\"xmgcuc001\" value='10086'/>";
        xmlStr += "          <Field name=\"xmgcuc015\" value='GCXMSQ-20200226028'/>";
        xmlStr += "         </Record>";
        xmlStr += "        </Master>";
        xmlStr += "      </RecordSet>";
        xmlStr += "     </Document>";
        xmlStr += "    </RequestContent>";
        xmlStr += "   </Request>";
        xmlStr += "   ]]>";
        xmlStr += "  </param>";
        xmlStr += " </payload>";

        System.out.println("XML:"+xmlStr);

        String demo = xmltst(xmlStr);
        System.out.println(demo);

    }

    public static String xmltst(String xmldemo){
        String str="";
        try{
            Document doc = null;
            doc= DocumentHelper.parseText(xmldemo);
            Element rootElt = doc.getRootElement();
            System.out.println("1:"+rootElt.asXML());

            Element param = rootElt.element("param");
            System.out.println("2:"+param.asXML());

            Document doc2 = null;
            doc2 = DocumentHelper.parseText(param.getStringValue());
            Element paramroot = doc2.getRootElement();
            System.out.println("3:"+paramroot.asXML());

            Element request = paramroot.element("RequestContent");
            //System.out.println("4:"+request.getName());

            Element rd = request.element("Document");
            //System.out.println("5:"+rd.getName());

            Element rest = rd.element("RecordSet");
            //System.out.println("6:"+rest.getName());

            Element mase = rest.element("Master");
            //System.out.println("7:"+mase.getName());

            Element red = mase.element("Record");
            System.out.println("8:"+red.asXML());

            Iterator it = red.elementIterator("Field");
            Element field = null;
            while(it.hasNext()){
                field = (Element)it.next();
                if(field.attributeValue("name").equals("xmgcuc015")) {
                    str = field.attributeValue("name");
                    str += ":"+field.attributeValue("value");
                }
            }
        }
        catch (DocumentException e){
            System.out.println("出错了:"+e.getMessage());
        }

        return str;
    }

}

输出:

XML: <payload> <param key="data" type="XML" > <![CDATA[ <Request> <RequestContent> <Document> <RecordSet id="1"> <Master name="xmgcuc_t" node_id="1"> <Record> <Field name="xmgcuc001" value='10086'/> <Field name="xmgcuc015" value='GCXMSQ-20200226028'/> </Record> </Master> </RecordSet> </Document> </RequestContent> </Request> ]]> </param> </payload>

1:<payload> <param key="data" type="XML"> <![CDATA[ <Request> <RequestContent> <Document> <RecordSet id="1"> <Master name="xmgcuc_t" node_id="1"> <Record> <Field name="xmgcuc001" value='10086'/> <Field name="xmgcuc015" value='GCXMSQ-20200226028'/> </Record> </Master> </RecordSet> </Document> </RequestContent> </Request> ]]> </param> </payload>

2:<param key="data" type="XML"> <![CDATA[ <Request> <RequestContent> <Document> <RecordSet id="1"> <Master name="xmgcuc_t" node_id="1"> <Record> <Field name="xmgcuc001" value='10086'/> <Field name="xmgcuc015" value='GCXMSQ-20200226028'/> </Record> </Master> </RecordSet> </Document> </RequestContent> </Request> ]]> </param>

3:<Request> <RequestContent> <Document> <RecordSet id="1"> <Master name="xmgcuc_t" node_id="1"> <Record> <Field name="xmgcuc001" value="10086"/> <Field name="xmgcuc015" value="GCXMSQ-20200226028"/> </Record> </Master> </RecordSet> </Document> </RequestContent> </Request>

8:<Record> <Field name="xmgcuc001" value="10086"/> <Field name="xmgcuc015" value="GCXMSQ-20200226028"/> </Record>

xmgcuc015:GCXMSQ-20200226028

 

参考文献:https://www.cnblogs.com/ysySelf/p/10186322.html

posted @ 2020-02-27 11:25  AaronLi  阅读(310)  评论(0编辑  收藏  举报