国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

利用java Api解析XML

2019-11-17 04:04:00
字體:
來源:轉載
供稿:網友
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
* Usage: You can get a instance of Document from a string of xml.
* This class supply some method to read a document.
*
*/
public class XMLParser {

/**
  * Get a instance of Document from a string of xml.
  *
  * @param xmlStr
  * @return Document
  */
public static Document parse(String xmlStr) {
  if (xmlStr == null) {
   return null;
  }
  StringReader reader = new StringReader(xmlStr);
  InputSource source = new InputSource(reader);
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = null;

  try {
   builder = factory.newDocumentBuilder();
   
  } catch (ParserConfigurationException e) {
   e.PRintStackTrace();
  }
  if(builder == null){
   return null;
  }
  Document doc = null;
  try {
   doc = builder.parse(source);
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return doc;
}


/**
  * Get Item Value.
  * @param node
  * @return String
  */
public static String getItemValue(Node node){
  String value = "";
  if(node == null){
   return null;
  }
  node = node.getFirstChild();
  if(node != null){
   value = node.getNodeValue();
  }
  return value.trim();
}

/**
  * Get Simple Node's Value
  *
  * @param list
  * @return String
  */
public static String getSimpleNodeValue(NodeList list){
  String value = "";
  Node node = null;
  if (list != null) {
   node = list.item(0);
   value = getItemValue(node);
  }
  return value.trim();
}

/**
  * Get Children.
  * @param list
  * @return List<Node>
  */
public static List<Node> getChildren(NodeList list){
  List<Node> children = new ArrayList<Node>();
  Node node = null;
  if(list == null){
   return children;
  }
  for(int i= 0; i< list.getLength(); i++){
   node = list.item(i);
   if (node.getNodeType() == Node.ELEMENT_NODE) {
    children.add(node);
   }
  }
  return children;
}


/**
  * Get Attribute Value
  * @param node
  * @param name
  * @return String
  * @throws Exception
  */
public static String getAttributeValue(Node node, String name) throws Exception{
  String value = "";
  if(node == null){
   throw new Exception("Node is null!");
  }
  NamedNodeMap nnm = node.getAttributes();
  if (nnm == null) {
   throw new Exception("NamedNodeMap is null!");
  }
  Node item = nnm.getNamedItem(name);
  if (item == null) {
   throw new Exception("No such attribute.");
  }
  value = item.getNodeValue();
  return value.trim();
}

}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保德县| 乐亭县| 西充县| 黑龙江省| 都江堰市| 磐石市| 南皮县| 江津市| 平南县| 桃园市| 岗巴县| 鹿泉市| 资溪县| 商城县| 曲周县| 延长县| 筠连县| 吉林市| 龙泉市| 涟源市| 桐城市| 云林县| 和硕县| 鄂州市| 中超| 赣州市| 东兰县| 拉萨市| 来凤县| 保山市| 苗栗县| 米泉市| 怀化市| 庄浪县| 石台县| 当阳市| 潜江市| 永修县| 班戈县| 平定县| 银川市|