DOM解析XML文档(深度与广度)

解析方式一:

package cn.com.csuinfosoft.dom;

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;

/**
 * title:dom parser xml
 * @author Administrator
 *
 */
public class ParserDom {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  try{
   
   //1:创建DocumentBuilderFactory
   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
   
   //2:DocumentBuilder
   DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
   
   //3:加载XML文档在内存中生成文档树
   Document document = documentBuilder.parse( "bookinfo.xml" );
   
   //获取根节点  bookinfo
   Element root = document.getDocumentElement();
   
   //获取所有子节点集合
   NodeList bookList = root.getElementsByTagName( "book" );
   NodeList bookName = root.getElementsByTagName( "bookname" );
   NodeList author = root.getElementsByTagName( "author" );
   NodeList price = root.getElementsByTagName( "price" );
   NodeList age = root.getElementsByTagName( "age" );
   NodeList type = root.getElementsByTagName( "type" );
   
   for( int i = 0; i < bookList.getLength();i++ ){
    
    Node bookNode = bookList.item( i );
    
    //获取属性
    System.out.println( "id:"+bookNode.getAttributes().getNamedItem( "id" ).getTextContent() );
    
    //获取内容
    System.out.println( "bookname:"+bookName.item( i ).getTextContent() );
    System.out.println( "author:"+author.item( i ).getTextContent() );
    System.out.println( "price:"+price.item( i ).getTextContent() );
    System.out.println( "age:"+age.item( i ).getTextContent() );
    System.out.println( "type:"+type.item( i ).getTextContent() );
    System.out.println( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" );
    
   }
   
  }catch( Exception e ){
   
   e.printStackTrace();
  }
 }
}
 

 

解析方式二:

package cn.com.csuinfosoft.dom;

import java.io.IOException;

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 org.xml.sax.SAXException;

/**
 * title:dom parser xml
 * @author Administrator
 *
 */
public class TestDom {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  try {
   //1:创建DocumentBuilderFactory
   DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
   
   //2:DocumentBuilder
   DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
   
   //3:加载XML文档在内存中生成文档树
   Document document = documentBuilder.parse( "bookinfo.xml" );
   
   //获取根节点  bookinfo
   Element root = document.getDocumentElement();
   
   //获取根节点下的第一级子节点集合 book
   NodeList nodeList = root.getChildNodes();
   
   System.out.println( "nodeList length:"+nodeList.getLength() );
   
   for( int i = 0; i < nodeList.getLength(); i++ ){
    
    Node node = nodeList.item( i );
    
    //System.out.println( "nodeName:"+node.getNodeName() +" nodeType:"+node.getNodeType() );
    //处理book节点,
    if( node.getNodeType() == Node.ELEMENT_NODE ){
     
     //获取book节点中的id属性值
     System.out.println( "id:"+node.getAttributes().getNamedItem( "id" ).getTextContent() );
     //获取book节点下的子节点集合
     NodeList childList = node.getChildNodes();
     
     for( int j = 0; j < childList.getLength(); j++ ){
      
      Node childNode = childList.item( j );
      
      //处理book下的子节点
      if( childNode.getNodeType() == Node.ELEMENT_NODE ){
       
       //输出内容
       System.out.println( "nodeName:"+childNode.getNodeName() );
       System.out.println( "nodeValue:"+childNode.getTextContent() );
  
       System.out.println( "++++++++++++++++++++++++++++++++++++++++++++" );
    
      }
     }
    }
   }
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block ParserConfigurationException
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}
 

 

输出XML文档:

package cn.com.csuinfosoft.dom;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * title:构造XML,输出
 * @author Administrator
 *
 */
public class TransformerXml {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  
  try {
   
   //1:
   DocumentBuilderFactory documentBuilderFacotry = DocumentBuilderFactory.newInstance();
   
   //2:
   DocumentBuilder documentBuilder = documentBuilderFacotry.newDocumentBuilder();
   
   //Document创建一个空的文档树,然后添加数据
   Document doc = documentBuilder.newDocument();
   
   //Element 节点,标记
   Element root = doc.createElement( "bookinfo" );//创建根节点
   
   //创建book
   Element book = doc.createElement( "book" );
   
   //添加id属性
   book.setAttribute( "id", "101" );
   
   //bookname
   Element bookname = doc.createElement( "bookname" );
   
   //添加内容
   bookname.setTextContent( "java编程思想" );
   
   //appendChild
   book.appendChild( bookname );
   root.appendChild( book );
   doc.appendChild( root );
   //==================
   //将树输出到xml文件在去
   TransformerFactory factory = TransformerFactory.newInstance();
   Transformer transformer = factory.newTransformer();
   
   //Source
   DOMSource source = new DOMSource( doc );
   StreamResult result = new StreamResult( new FileOutputStream( "book.xml" ) );
   
   //输出
   transformer.transform( source, result );
   
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (TransformerConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (TransformerException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

}
 

你可能感兴趣的:(xml,Web,职场,休闲,xml学习)