My Java Blog Park

简单的DOM(java)的操作

package wlz.xml;

import javax.xml.parsers.*;
import org.w3c.dom.*;
//import javax.xml.transform.*;
//import javax.xml.transform.dom.DOMSource;
//import javax.xml.transform.stream.StreamResult;
import java.io.*;
import org.apache.xml.serialize.*;

public class WriteXml {
    public static void writeXml(Document doc,String filename) throws Exception{
        /*TransformerFactory tf=TransformerFactory.newInstance();
        Transformer f=tf.newTransformer();
        //f.setOutputProperties();
        DOMSource source=new DOMSource(doc);
        StreamResult result=new StreamResult(new File(filename));
        f.transform(source,result);*/
        
        FileOutputStream fos = new FileOutputStream(filename);
        OutputFormat of = new OutputFormat("XML","GB2312",true);
        of.setIndent(2);
        of.setIndenting(true);
        XMLSerializer serializer = new XMLSerializer(fos,of);
        
        serializer.asDOMSerializer();
        serializer.serialize(doc.getDocumentElement());
        fos.close();
    }
    
    public static void outputElement(Document doc,String elementName){
        NodeList list= doc.getElementsByTagName(elementName);
        System.out.println("------------------------------------------");
        for(int i=0;i<list.getLength();i++){
            System.out.println(elementName+"="+list.item(i).getFirstChild().getNodeValue()); //取出元素的值
        }
        System.out.println("------------------------------------------");
    }
    
    public static void addElement(Document doc,Element root,String name,String age,String sex){
        Element student=doc.createElement("student");
        Element ename=doc.createElement("name");
        Element eage=doc.createElement("age");
        Element esex=doc.createElement("sex");
              
        ename.appendChild(doc.createTextNode(name));
        eage.appendChild(doc.createTextNode(age));
        esex.appendChild(doc.createTextNode(sex));
        
        student.appendChild(ename);
        student.appendChild(eage);
        student.appendChild(esex);
        
        root.appendChild(student);
    }
    
    public static Document createDocument() throws Exception{
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document doc=db.newDocument();
        return doc;
    }
    
    public static void main(String[] args) throws Exception{
/*
output the xml
<class name="计算机1班">
    <student>
       <name>
       <age>
       <sex>
    </student>
    <student>
       <name>
       <age>
       <sex>
    </student>
</class>

 */    
        Document doc=createDocument();
        doc.createProcessingInstruction("encoding","gb2312");
        Element root=doc.createElement("class");
        root.setAttribute("name","计算机1班");
        doc.appendChild(root);
        
        addElement(doc,root,"黄蓉","30","女");
        addElement(doc,root,"郭靖","32","男");
        addElement(doc,root,"杨过","8","男");
        
        outputElement(doc,"name");
        outputElement(doc,"sex");
        
        writeXml(doc,"mydomxml.xml");
        System.out.println("output ok.");
                
    }
}

posted on 2006-09-26 14:36 2195113 阅读(186) 评论(0)  编辑  收藏 所属分类: XML (SAX & DOM & JDOM)


只有注册用户登录后才能发表评论。


网站导航: