Java处理XML的DOM方式

person.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persons>
 <person id="20">
  <name>rock</name>
  <age>18</age>
  <address>beijing</address>
 </person>
 <person id="21">
  <name>iori</name>
  <age>20</age>
  <address>riben daban</address>
 </person>
 <person id="22">
  <name>mai</name>
  <age>21</age>
  <address>riben dongjing</address>
 </person>
</persons>

 

Person.java

package cn.rock.bean;

public class Person {
 private int id;
 private String name;
 private short age;
 private String address;
 
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public short getAge() {
  return age;
 }
 public void setAge(short age) {
  this.age = age;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 @Override
 public String toString() {
  return "[id:" + id + " name:" + name + " age:" + age + " address:" + address + "]";
 }
 
}

 

 

package cn.rock.service;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import cn.rock.bean.Person;

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

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

}

 

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