Java解析xml相關

/**
  * @函數名稱:${getXmlDoc}
  * @創建日期:${2008-11-13}
  * @功能說明:得到要解析的xml文件對象
  * @參數說明:xml文件的路徑
  * @返回說明:文件對象
  */

public Document getXmlDoc(String filePath) throws Exception{  
  DOMParser domparser = new DOMParser();
   InputSource inputsource = new InputSource();
   FileInputStream is= new FileInputStream(filePath);
   inputsource.setByteStream(is);
   domparser.parse(inputsource);
   xmlDoc =domparser.getDocument();
   return xmlDoc;
 }

/**
  * @函數名稱:${getXmlData}
  * @創建日期:${2008-11-13}
  * @功能說明:解析xml文件獲取節點的值
  * @參數說明:節點名稱
  * @返回說明:返回xml某個節點的值
  */

 public String getXmlData(String tagName) throws Exception{  
  String xmlData="";
  Element elem=(Element) xmlDoc.getDocumentElement();
  xmlData=getElementByTagName(elem, tagName);  
  return xmlData;
 }

/**
  * @函數名稱:${getElementByTagName}
  * @創建日期:${2008-11-13}
  * @功能說明:輔助方法,根據節點和節點名稱得到節點值
  * @參數說明:父節點、子節點名稱
  * @返回說明:節點值
  */
 public String getElementByTagName(Element parentEle, String tagName)throws Exception{
   String retVal= null;
   NodeList theNl = (parentEle).getElementsByTagName(tagName);
  if(theNl.getLength()>0 ){
   if(theNl.item(0).getFirstChild()!=null){
    retVal = theNl.item(0).getFirstChild().getNodeValue();
   }
  }
   return retVal;
  }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章