XML解析(DOM解析)

1、需要的jar包

dom4j.jar


2、需要引入的包
import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;


xml格式
    
/** @param paramsXml **/
<?xml version="1.0" encoding="UTF-8"?> 
 <request> 
     <queryType>batch</queryType> 
     <param>
          <savePath>path/to/wave/folder</savePath>
          <idList>
             ZX20130822201308221540390001001000,ZX20130821201308212047190001001000
          </idList> 
     </param>
          </request>
/**@return **/



3、解析代码
   String paramsXml;// 具有XML格式的字符串


   Document document = null;


   document = DocumentHelper.parseText(paramsXml);


   /*path/to/wave/folder*/

   Element savePathEle = (Element) document.selectSingleNode(&quot;//param/savePath&quot;);

    

   /*获取元素的值,在这里获取元素savePath的值:path/to/wave/folder*/

   String uploadFilePath = savePathEle.getTextTrim();

  

   /*XXXXXXX*/

   Element idListEle = (Element) document.selectSingleNode(&quot;//param/idList&quot;);

  

  /*获取元素的值,在这里获取元素idList的值:XXXXXXr*/

   String strkey = idListEle.getTextTrim();





 

你可能感兴趣的:(xml解析)