当前位置:编程学习 > XML/UML >>

Dom解析XML常见的操作

XmlArgs.java文件:
//常量类
public class XmlArgs {
 public static final String XML_HR = "lib/xml/hr.xml";
 public static final String XML_HRD = "lib/xml/hr.dtd";
 public static final String XML_STU = "lib/xml/students.xml";
 public static final String XML_BOOK = "lib/xml/books.xml";
}
 
DomOper.java文件:
import java.io.File; www.zzzyk.com
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Entity;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Notation;
public class DomOper {
 public static void main(String[] args) {
  DomOper.createXml();
  //DomOper.readXml();
  //DomOper.printXml(1);
  //DomOper.printXml(2);
  //DomOper.addNode();
  //DomOper.delNode();
  //DomOper.upNode();
 }
 public static void createXml(){
  try{
   DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   Document doc = builder.newDocument();
   Element root = doc.createElement("老师");
   Element wNode = doc.createElement("王");
   wNode.appendChild(doc.createTextNode("我是王老师"));
   root.appendChild(wNode);
   doc.appendChild(root);
   Transformer trans = TransformerFactory.newInstance().newTransformer();
   trans.setOutputProperty(OutputKeys.ENCODING,"utf-8");
   trans.setOutputProperty(OutputKeys.INDENT,"yes");
   trans.transform(new DOMSource(doc),new StreamResult("C:/new.xml"));
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 public static void readXml(){
  try{
   DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
   Document doc = builder.parse(new File(XmlArgs.XML_HR));
   DocumentType docType = doc.getDoctype();
   if(docType!=null){
    System.out.println("DTD名称:"+docType.getName());
    System.out.println("DTD公共ID:"+docType.getPublicId());
    System.out.println("DTD系统ID:"+docType.getSystemId());
    System.out.println("DTD内部子集:"+docType.getInternalSubset());
    NamedNodeMap ens = docType.getEntities();
    for(int i=0;i<ens.getLength();i++){
     Entity en = (Entity)ens.item(i);
     System.out.println("第"+(i+1)+"个实体");
     System.out.println("实体名称:"+en.getNodeName());
     System.out.println("标记名称:"+en.getNotationName());
     System.out.println("实体公共ID:"+en.getPublicId());
     System.out.println("实体系统ID:"+en.getSystemId());
    }
    NamedNodeMap nos = docType.getNotations();
    for(int j=0;j<nos.getLength();j++) {
     Notation no = (Notation)nos.item(j);
     System.out.println("第"+(j+1)+"个标记");
     System.out.println("标记名称:"+no.getNodeName());
     System.out.println("标记公共ID:"+no.getPublicId());
     System.out.println("标记系统ID:"+no.getSystemId());
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 public static void printXml(int type){
  try{
   DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
   fac.setNamespaceAware(true);
   Document doc = null;
   if(type==1){
    doc = fac.newDocumentBuilder().parse(new File(XmlArgs.XML_BOOK));
    DomOper.print(doc.getDocumentElement());
   }else if(type==2){
    doc = fac.newDocumentBuilder().parse(new File(XmlArgs.XML_STU));
    DomOper.printN(doc.getDocumentElement());
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 public static void print(Node node) {
  short type = node.getNodeType();
  switch(type){
   case Node.ELEMENT_NODE:
    System.out.println("--------元素节点--------");
    if(node.getNamespaceURI()!=null){
     System.out.println("元素名称:"+ node.getNodeName());
     System.out.println("本地名称:"+ node.getLocalName());
     System.out.println("命名空间前缀:"+node.getPrefix());
     System.out.println("命名空间URL:"+node.getNamespaceURI());
    }
    Node nd = null;
    if(node.hasAttributes()){
     NamedNodeMap attrs = node.getAttributes();
     for(int i=0;i<attrs.getLength();i++){
      nd = attrs.item(i);
      if(nd.getNamespaceURI()!=null){
       print(nd);
      }
     }
    }
    nd = node.getFirstChild();
    while(nd!=null){
     print(nd);
     nd = nd.getNextSibling();
    }
    break;
   case Node.ATTRIBUTE_NODE:
    System.out.println("--------属性节点--------");
    System.out.println("属性名称:"+ node.getNodeName());
    System.out.println("本地名称:"+ node.getLocalName());
    System.out.println("命名空间前缀:"+node.getPrefix());
    System.out.println("命名空间URL:"+node.getNamespaceURI());
    break;
   default:break;
  }
 }
 public static void printN(Node node) {
  short nodeType = node.getNodeType();
  switch(nodeType){
   case Node.PROCESSING_INSTRUCTION_NODE:
    System.out.println("
补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,