java解析xml數據

//xml文檔 testDOM.xml

<?xml version="1.0" encoding="gb2312"?>
<students>
<student id="1" name="張三">
 <age>18</age>
 <sex>男</sex>
</student>

<student id="2" name="裏司">
 <age>16</age>
 <sex>女</sex>
</student>

<student id="3" name="王五">
 <age>19</age>
 <sex>男</sex>
</student>

</students>

 

 

 

//src源文件

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.jdom.input.SAXBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 功能:
 * @author yaos  email: [email protected]
 * @date 2008 11 25
 */
public class TestParseDOM {

 /**
  * 功能:
  * @param args
  */

 public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
  
  DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
  DocumentBuilder db = domfac.newDocumentBuilder();
  Document doc = db.parse(new File("src/testDOM.xml"));//裝載.xml文件 路徑在default package下
  Element root = doc.getDocumentElement();//獲得根元素
  NodeList nodelist = root.getChildNodes();//獲得根元素下面的 所有節點的list集合
  List list = new ArrayList();//存放解析後的結果
  if(nodelist!=null){
   for(int i=0;i<nodelist.getLength();i++){//遍歷節點list集合
    Node node = nodelist.item(i);//獲得第i個節點
    if(node.getNodeType()==Node.ELEMENT_NODE){
//     Method 1.如果知道這一級有多少個attribute但是不知道attribute的名字
     String id = node.getAttributes().item(0).getNodeName();
     String idValue = node.getAttributes().item(0).getNodeValue();
     String name = node.getAttributes().item(1).getNodeName();
     String nameValue = node.getAttributes().item(1).getNodeValue();
     Map map = new HashMap();
     map.put(id, idValue);
     map.put(name, nameValue);
     list.add(map);
     
//     Method 2.如果知道這一級有多少個attribute並且知道attribute的名字
//     String id = node.getAttributes().getNamedItem("id").getNodeName();
//     String idValue = node.getAttributes().getNamedItem("id").getNodeValue();
//     String name = node.getAttributes().getNamedItem("name").getNodeName();
//     String nameValue = node.getAttributes().getNamedItem("name").getNodeValue();
//     Map map = new HashMap();
//     map.put(id, idValue);
//     map.put(name, nameValue);
//     list.add(map);
     
//     Method 3.如果不知道這一級有多少個attribute並且不知道attribute的名字     
//     NamedNodeMap nodeMap = node.getAttributes();
//     for(int j = 0 ; j < nodeMap.getLength();j++){
//      String key = nodeMap.item(j).getNodeName();
//      String value = nodeMap.item(j).getNodeValue();
//      Map keyValueMap = new HashMap();
//      keyValueMap.put(key, value);
//      list.add(keyValueMap);
//     }
     
     
     for(Node child=node.getFirstChild();child!=null;child=child.getNextSibling()){//遍利當前節點下的節點,(指向第一個節點;如果節點不爲空;節點指向下一個兄弟節點)
      if(child.getNodeType()==Node.ELEMENT_NODE){//如果節點是 節點類型
       String childName  =  child.getNodeName();//獲得的節點的名字(age或是sex)
       String childValue = child.getFirstChild().getNodeValue();//獲得節點的值(注意使用的是 第一個孩子節點的value而不是直接取value,即age或sex對應的值)
//       String childValue = child.getTextContent();//使用此方法獲得的與上面相同的效果,具體使用不明確
       
//       如果age和sex節點裏面還有attrbitue的話可以使用以下方式編歷
//       <student id="1" name="張三">
//        <age a="A">18</age>
//        <sex b="B">男</sex>
//       </student>
//       String tempName = child.getAttributes().item(0).getNodeName();
//       String tempValue = child.getAttributes().item(0).getNodeValue();
//       System.out.println(tempName+"___"+tempValue);
       
       Map map1 = new HashMap();
       map1.put(childName, childValue);
       list.add(map1);
      }
     }
    }
   }
  }
  
  for(int i = 0 ; i < list.size() ; i ++){
   Map map = (Map) list.get(i);
   for(Object obj : map.keySet()){
    System.out.println(obj.toString()+"-->"+map.get(obj));
   }
   
  }
 }

}

 

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