zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    webpack优化
    原生js实现平滑滚动
    【大前端攻城狮之路】JavaScript函数式编程
    【大前端攻城狮之路】面试集锦
    【大前端攻城狮之路·二】Javascript&QA⼯程师
    【vue系列之三】从一个vue-pdf-shower,说说vue组件和npm包
    display:box和display:flex填坑之路
    【vue系列之一】使用vue-cli脚手架工具搭建vue-webpack项目
    快速上手微信小程序-快递100
    js面向对象小结(工厂模式,构造函数,原型方法,继承)
  • 原文地址:https://www.cnblogs.com/xiaoli9627/p/12371131.html
Copyright © 2011-2022 走看看