DOM实例之一

 DOM

import java.io.File;
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;

public class DOMStudentsInfo 
{

    
public static void main(String[] args) 
    
{
        DocumentBuilderFactory dbf
=DocumentBuilderFactory.newInstance();
        System.out.println(dbf.getClass().getName());
        
        
try 
        
{
            DocumentBuilder db
=dbf.newDocumentBuilder();
            Document doc
=db.parse(new File("students.xml"));
            
            NodeList nl
=doc.getElementsByTagName("student");
            
int len=nl.getLength();
            
for(int i=0;i<len;i++)
            
{
                Element eltStu
=(Element)nl.item(i);
              
                Node eltName
=eltStu.getElementsByTagName("name").item(0);
                Node eltAge
=eltStu.getElementsByTagName("age").item(0);
            
                String name
=eltName.getFirstChild().getNodeValue();
                String age
=eltAge.getFirstChild().getNodeValue();
                
                System.out.println(
"name:"+name);
                System.out.println(
"age:"+age);
                System.out.println(
"---------------------");
            }

        }
 
        
catch (ParserConfigurationException e) 
        
{
            e.printStackTrace();
        }
 
        
catch (SAXException e) 
        
{
            e.printStackTrace();
        }
 
        
catch (IOException e) 
        
{
            e.printStackTrace();
        }

    }


}

XML

<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
  
<student sn="01">
     
<name>张三</name>
     
<age>78</age>
  
</student>
  
<student sn="02">
     
<name>李死</name>
     
<age>42</age>
  
</student>
</students>

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class DOMPrinter
{

    
public static void printNodeInfo(Node node)
    
{
        System.out.println(node.getNodeName()
+" : "+node.getNodeValue());
    }

    
    
public static void printNode(Node node)
    
{
        
short nodeType=node.getNodeType();
        
switch(nodeType)
        
{
        
case Node.PROCESSING_INSTRUCTION_NODE:
            System.out.println(
"-----------PI start-----------");
            printNodeInfo(node);
            System.out.println(
"-----------PI end-----------");
            
break;
        
case Node.ELEMENT_NODE:
            System.out.println(
"-----------Element start-----------");
            printNodeInfo(node);
            System.out.println(
"-----------Element end-----------");
            
            NamedNodeMap attrs
=node.getAttributes();
            
int attrNum=attrs.getLength();
            
for(int i=0;i<attrNum;i++)
            
{
                Node attr
=attrs.item(i);
                System.out.println(
"-----------Attribute start-----------");
                printNodeInfo(attr);
                System.out.println(
"-----------Attribute end-----------");
            }

            
break;
        
case Node.TEXT_NODE:
            System.out.println(
"-----------Text start-----------");
            printNodeInfo(node);
            System.out.println(
"-----------Text end-----------");
            
break;
        
default:
            
break;
        }

        
        Node child
=node.getFirstChild();
        
while(child!=null)
        
{
            printNode(child);
            child
=child.getNextSibling();
        }

    }

    
    
public static void main(String[] args)
    
{
        
        DocumentBuilderFactory dbf
=DocumentBuilderFactory.newInstance();
        
        
try
        
{
            DocumentBuilder db
=dbf.newDocumentBuilder();
            Document doc
=db.parse(new File("students.xml"));
            
//DOMPrinter domPrinter=new DOMPrinter();
            
//domPrinter.printNode(doc);
            
            printNode(doc);
            
        }

        
catch (ParserConfigurationException e)
        
{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
catch (SAXException e)
        
{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
catch (IOException e)
        
{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }


    }



}

XML

<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
  
<student sn="01">
     
<name>张三</name>
     
<age>78</age>
  
</student>
  
<student sn="02">
     
<name>李死</name>
     
<age>42</age>
  
</student>
</students>

import java.io.File;
import java.io.IOException;

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;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class DOMConvert
{

    
public static void main(String[] args) 
    
{
        DocumentBuilderFactory dbf
=DocumentBuilderFactory.newInstance();
        
try 
        
{
            DocumentBuilder db
=dbf.newDocumentBuilder();
            Document doc
=db.parse(new File("students.xml"));
            
            Element eltStu
=doc.createElement("student");
            Element eltName
=doc.createElement("name");
            Element eltAge
=doc.createElement("age");
            
            Text textName
=doc.createTextNode("王五");
            Text textAge
=doc.createTextNode("18");
            
            eltName.appendChild(textName);
            eltAge.appendChild(textAge);
            
            eltStu.appendChild(eltName);
            eltStu.appendChild(eltAge);
            eltStu.setAttribute(
"sn","03");
            
            Element root
=doc.getDocumentElement();
            root.appendChild(eltStu);
            
            NodeList nl
=root.getElementsByTagName("student");
            root.removeChild(nl.item(
0));
            
            Element eltStuChg
=(Element)nl.item(0);
            Node nodeAgeChg
=eltStuChg.getElementsByTagName("age").item(0);
            nodeAgeChg.getFirstChild().setNodeValue(
"22");
        
            
int len=nl.getLength();
            
for(int i=0;i<len;i++)
            
{
                Element elt
=(Element)nl.item(i);
                System.out.println(
"编号:" + elt.getAttribute("sn"));
                
                Node nodeName
=elt.getElementsByTagName("name").item(0);
                Node nodeAge
=elt.getElementsByTagName("age").item(0);
                
                String name
=nodeName.getFirstChild().getNodeValue();
                String age
=nodeAge.getFirstChild().getNodeValue();
                
                System.out.println(
"姓名:"+name);
                System.out.println(
"年龄:" + age);
                
                System.out.println(
"--------------------");
                
            }

            
            TransformerFactory tff
=TransformerFactory.newInstance();
            Transformer tf
=tff.newTransformer();
            tf.setOutputProperty(
"encoding","gb2312");
            DOMSource source
=new DOMSource(doc);
            StreamResult result
=new StreamResult(new File("convert.xml"));
            tf.transform(source,result);
            
        }

        
catch (ParserConfigurationException e) 
        
{
            e.printStackTrace();
        }
 
        
catch (SAXException e) 
        
{
            e.printStackTrace();
        }
 
        
catch (IOException e) 
        
{
            e.printStackTrace();
        }
 
        
catch (TransformerConfigurationException e)
        
{
            e.printStackTrace();
        }

        
catch (TransformerException e) 
        
{
            e.printStackTrace();
        }

    }


}

XML

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
  
  
<student sn="02">
     
<name>李死</name>
     
<age>22</age>
  
</student>
<student sn="03"><name>王五</name><age>18</age></student></students>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值