DOM解析XML笔记

package com.vmm.xml;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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.xml.sax.InputSource;

public class XMLFactory {

 private static DocumentBuilderFactory domfac;
 private static DocumentBuilder dombuilder;
 private static XMLFactory instance;
 private static TransformerFactory tranFactory;
 private static Transformer aTransformer;
 private XMLFactory() {

  domfac = DocumentBuilderFactory.newInstance();
  tranFactory = TransformerFactory.newInstance();
  try {
   dombuilder = domfac.newDocumentBuilder();
   aTransformer = tranFactory.newTransformer();
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (TransformerConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public static XMLFactory getInstance() {
  if (instance == null) {
   instance = new XMLFactory();

  }
  return instance;
 }

 public String getXMLString(Document doc) {
  String ret;
  Source src = new DOMSource(doc);
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  Result dest = new StreamResult(outputStream);
  try {
   aTransformer.transform(src, dest);
  } catch (TransformerException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return null;
  }
  
  ret=outputStream.toString();
  if(ret.startsWith("<?")){
   return ret.substring(ret.indexOf(">")+1);
  }
  return ret;
 }

 public String getValue(String xmlString, String tagName) {
  try {
   StringReader sr = new StringReader(xmlString);
   InputSource is = new InputSource(sr);
   Document doc = dombuilder.parse(is);
   return doc.getElementsByTagName(tagName).item(0).getTextContent();

  } catch (Exception e) {
   // TODO: handle exception
   return null;
  }
 }
}

 

package com.vmm.socket;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

 

 

 

public class VMMSocket {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
    DocumentBuilderFactory domfac = DocumentBuilderFactory
             .newInstance();
     // 从DOM工厂获得DOM解析器
   
     DocumentBuilder dombuilder = domfac.newDocumentBuilder();
     Document document=dombuilder.newDocument();
   
     Element r1=document.createElement("av");
     Element e=document.createElement("b");
    
     document.appendChild(r1);
     r1.appendChild(e);
    // System.out.println("xml:"+XMLFactory.getInstance().getXMLString(document));
     TransformerFactory tranFactory = TransformerFactory.newInstance();
     Transformer aTransformer = tranFactory.newTransformer();
     Source src = new DOMSource(document);
     ByteArrayOutputStream  outputStream  =  new  ByteArrayOutputStream();  
     Result dest = new StreamResult(outputStream);
     aTransformer.transform(src, dest);
     System.out.println("----:"+outputStream.toString());
     // 把要解析的XML文档转化为输入流,以便DOM解析器解析它
     StringReader sr=new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml1 a='g' b='gf'><b>dg</b><a><b>ccedd</b><b>ccedd</b></a></xml1>");
     InputSource is=new InputSource(sr);
     //转换document
     String xmlString="<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml1 a='g' b='gf'><b>dg</b><a><b>ccedd</b><b>ccedd</b></a></xml1>";
    //System.out.println(XMLFactory.getInstance()getValue(xmlString, "b"));
    
     // 解析XML文档的输入流,得到一个Document

     Document doc = dombuilder.parse(is);
    
     // 得到XML文档的根节点(books)

     Element root = doc.getDocumentElement();
     System.out.println("root:"+root.toString());
     System.out.println("b:"+root.getElementsByTagName("b").item(0).getTextContent());
     // 获得根节点的所有属性名和值
     System.out.println(root.getChildNodes().item(0).getChildNodes().item(0)+","+root.getAttributes().getLength());
     if (0 < root.getAttributes().getLength()) {
         System.out.println("根节点属性信息..........");
         for (int a = 0; a < root.getAttributes().getLength(); a++) {
             System.out.println(root.getAttributes().item(a)
                     .getNodeName()
                     + ":" + root.getAttributes().item(a).getNodeName());

         }
     }

   //System.out.println(SocketRequest.getResult("afd 城需要bcedgdsf城富商大贾"));
   System.out.println("over");
  
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

 

你可能感兴趣的:(dom解析xml)