• 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

  • 相关阅读:
    【数量技术宅|金融数据分析系列分享】为什么中证500(IC)是最适合长期做多的指数
    异常控制流
    链接
    最小生成树的Prim算法(待修正版)
    最小生成树的Kruskal算法
    优先队列用法(转载)
    不相交集合的链表实现
    寻找通用汇点
    找零问题
    【Angular06】管道(类似vue的过滤器)、变更检测的工作原理
  • 原文地址:https://www.cnblogs.com/xiaoli9627/p/12371131.html
Copyright © 2020-2023  润新知