Java处理XML的DOM方式2

package cn.rock.service;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cn.rock.bean.Person;

public class DOMParseService2 {
 private List<Person> personList = null;
 

 /**
  * @param args
  */
 public void DOMParseXML() {
  personList = new ArrayList<Person>();
  
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  try {
   DocumentBuilder builder = factory.newDocumentBuilder();
   FileInputStream fileInputStream = new FileInputStream("E:\\Workspaces\\MyEclipse for Spring 8.6\\xml\\bin\\person.xml");
   Document document = builder.parse(fileInputStream);
   
   Element root = document.getDocumentElement();
   
   NodeList persons = root.getElementsByTagName("person");
   
   for(int i=0; i<persons.getLength(); i++){
    Node personNode = persons.item(i);
    if(personNode instanceof Element){
     Element personElement = (Element)personNode;
     Person person = new Person();
     person.setId(new Integer(personElement.getAttribute("id")));
     
     NodeList childNodes = personElement.getChildNodes();
     
     for(int y=0; y<childNodes.getLength(); y++){
      if(childNodes.item(y).getNodeType() == Node.ELEMENT_NODE){
       Element childElement = (Element)childNodes.item(y);
       if("name".equals(childElement.getTagName())){
        String name = childElement.getFirstChild().getNodeValue().trim();
        System.out.println("name: " + name);
        person.setName(name);
       }else if("age".equals(childElement.getTagName())){
        String age = childElement.getFirstChild().getNodeValue().trim();
        System.out.println("age: " + age);
        person.setAge(new Short(age));
       }else if("address".equals(childElement.getTagName())){
        String address = childElement.getFirstChild().getNodeValue().trim();
        System.out.println("address: " + address);
        person.setAddress(address);
       }
      }
     }
     personList.add(person);
     
     
//     System.out.println(personElement.getFirstChild());
//     System.out.println(personElement.getNextSibling());
//     for(Node childNode = personElement.getFirstChild(); childNode != null; childNode.getNextSibling()){
//      System.out.println(childNode.getNodeName() + "---" + childNode.getNodeValue() + "---" + childNode.getNodeType() );
//      if(childNode.getNodeType() == Node.ELEMENT_NODE){
//       System.out.println("---------------------");
//      }
//     }
    }
   }
   
   Iterator<Person> iter = personList.iterator();
   while(iter.hasNext()){
    Person person = iter.next();
    System.out.println(person.toString());
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 
 }

}

你可能感兴趣的:(Java处理XML的DOM方式2)