现在的位置: 首页 > 综合 > 正文

java对xml全面解析,增,删,改,以及将java对象重新编组为xml文件

2013年10月04日 ⁄ 综合 ⁄ 共 4879字 ⁄ 字号 评论关闭
 

写了一个jaxp用dom处理xml的代码,具体例外省略,自己看吧,希望对你有帮助。
被解析的xml文件名为AnewXMLfile.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Document   : AnewXMLfile.xml
    Created on : 2007年3月13日, 下午3:36
    Author     : xhinker
-->
<customers>
  
<customer>
    
<id>001</id>
    
<name>xhinker</name>
    
<address>harbin</address>
  
</customer>
  
<customer>
    
<id>002</id>
    
<name>Car</name>
    
<address>Suzhou</address>
  
</customer>
  
<customer>
    
<id>003</id>
    
<name>Jimmy</name>
    
<address>ChengDu</address>
  
</customer>
  
<customer>
    
<id>004</id>
    
<name>Henry</name>
    
<address>Xi'an</address>
  
</customer>
</customers>

下面是用于解析这个xml文件的java代码,文件名为XMLfileReader.java

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.File;

import javax.xml.transform.dom.DOMSource;
import org.xml.sax.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.*;
/**
 * 
@author xhinker
 
*/

public class XMLfileReader {
    
/**
     *解组xml,使之成为Document对象
     
*/

    
public Document XMLParse(){
        
//建立解析器工厂
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        Document doc
=null;
        
try {
            DocumentBuilder db
=dbf.newDocumentBuilder();
            File file
=new File("AnewXMLfile.xml");
            doc
=db.parse(file);//解析xml文件
        }
 catch (Exception e) {}
        
return doc;
    }

    
/**
     *显示Docuument对象里的内容
     
*/

    
public void XMLreader(Document doc){     
        
try {
            System.out.println(
"haha");
            NodeList nl
=doc.getElementsByTagName("customer");
            
int len=nl.getLength();
            
for(int i=0;i<len;i++){
                Element customer
=(Element)nl.item(i);
                Node id
=customer.getElementsByTagName("id").item(0);
                Node name
=customer.getElementsByTagName("name").item(0);
                Node address
=customer.getElementsByTagName("address").item(0);
                String newid
=id.getFirstChild().getNodeValue();
                String newname
=name.getFirstChild().getNodeValue();
                String newAddress
=address.getFirstChild().getNodeValue();
                System.out.print(
"id号:");
                System.out.println(newid);
                System.out.print(
"名字:");
                System.out.println(newname);
                System.out.print(
"地址");
                System.out.println(newAddress);    
            }

        }
 catch (Exception e) {}
    }

    
/**
     *为Document对象里添加内容
     *
@param doc Document类型对象
     
*/

    
public void add(Document doc){
        
try {
            Element customer
=doc.createElement("customer");
            Element id
=doc.createElement("id");
            Element name
=doc.createElement("name");
            Element address
=doc.createElement("address");
           
            Text textId
=doc.createTextNode("005");
            Text textName
=doc.createTextNode("zhushudong");
            Text textAddress
=doc.createTextNode("haerbin");
           
            id.appendChild(textId);
            name.appendChild(textName);
            address.appendChild(textAddress);
           
            customer.appendChild(id);
            customer.appendChild(name);
            customer.appendChild(address);
           
            Element root
=doc.getDocumentElement();
            root.appendChild(customer);          
        }
 catch (Exception e) {}
    }

    
/**
     *删除结点
     *
@param doc Document对象
     *
@param i 用于指示要删除结点位置
     
*/

    
public void delete(Document doc,int i){
        NodeList nl
=doc.getElementsByTagName("customer");
        Node nodeDel
=nl.item(i);
        nodeDel.getParentNode().removeChild(nodeDel);
    }

    
/**
     *改变节点的值
     *
@param doc 传入的对象
     *
@param i 用于指示要修改结点位置
     *
@param attr 要修改节点的标记
     *
@param newValue 修改后的值
     
*/

    
public void change(Document doc,int i,String attr,String newValue){
        NodeList nl
=doc.getElementsByTagName("customer");
        Element cha
=(Element)nl.item(i);
        Node nodeForCha
=cha.getElementsByTagName(attr).item(0);
        nodeForCha.getFirstChild().setNodeValue(newValue);
    }

    
/**
     *将java对象转化成xml文件
     *
@param doc 要转化的java对象
     
*/

    
public void ObjectToXML(Document doc)throws TransformerConfigurationException{
        
try {
            
//利用文档节点创建一个DOM输入源
            DOMSource source=new DOMSource(doc);
            
//以newXMLfile.xml创建一个StreamResult对象
            StreamResult result=new StreamResult(new File("newXMLfile.xml"));
            
//创建转换器工厂对象
            TransformerFactory tff=TransformerFactory.newInstance();
            
//创建转换器对象
            Transformer tf=tff.newTransformer();
            
//进行转换
            tf.transform(source, result);
        }
 catch (Exception e) {}
    }

    
public static void main(String[] args){   
        XMLfileReader xmlReader
=new XMLfileReader();
        Document newdoc
=xmlReader.XMLParse();
        System.out.println(
"-----display------");
        xmlReader.XMLreader(newdoc);

抱歉!评论已关闭.