胖娃娃

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  4 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

  昨天学习了对XML的读操作,今天尝试修改XML中的内容,具体代码如下(SCXMLUtils.java):

package  SCXML;

import  java.io.FileWriter;

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  org.w3c.dom.Document;
import  org.w3c.dom.Node;
import  org.w3c.dom.NodeList;

public   class  SCXMLUtils  {

    
private  SCXMLUtils()  {
    }


    
public   static  Document getXMLDoc(String xmlfile)  {
        
try   {

            DocumentBuilderFactory factory 
=  DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder 
=  factory.newDocumentBuilder();
            Document doc 
=  builder.parse(xmlfile);
            
return  doc;

        }
  catch  (Exception e)  {

            System.out.println(
" an error occurred in SCXMLUtils: "   +  e);
            
return   null ;

        }


    }


    
public   static  Node getChild(Node parent, String name)  {

        NodeList children 
=  parent.getChildNodes();
        
int  len  =  children.getLength();
        Node node 
=   null ;
        
for  ( int  i  =   0 ; i  <  len; i ++ {

            node 
=  children.item(i);
            String thisname 
=  node.getNodeName();
            
if  (name.equals(thisname))  {

                
return  node;

            }


        }


        
return   null ;
    }


    
public   static  String getTextValue(Node node)  {

        
if  (   (node.getNodeType()  ==  Node.TEXT_NODE)
            
||  (node.getNodeType()  ==  Node.CDATA_SECTION_NODE))  {

            
return  node.getNodeValue();

        }
  else   {

            
return   null ;

        }


    }


    
public   static   void  setTextValue(Document xmldoc, Node node, String value)  {

        
if  (   (node.getNodeType()  ==  Node.TEXT_NODE)
            
||  (node.getNodeType()  ==  Node.CDATA_SECTION_NODE))  {

            
try   {
                TransformerFactory tf 
=  TransformerFactory.newInstance();
                Transformer tr 
=  tf.newTransformer();
                tr.setOutputProperty(
" encoding " " GBK " );
                node.setNodeValue(value);
                DOMSource ds 
=   new  DOMSource(xmldoc);
                StreamResult sr 
=   new  StreamResult( new  FileWriter( " test.xml " ));
                tr.transform(ds, sr);
            }
  catch  (Exception e)  {
                
                System.out.println(
" SCXMLUtils:  "   +  e);

            }

        }
  else   {

            System.out.println(
" SCXMLUtils: This node is not a text! " );

        }


    }


}


说明:最关键的变化就是增加了setTextValue这一个函数,其中tr.setOutputProperty("encoding", "GBK")很重要,否则写入中文会出现乱码, 
   node.setNodeValue(value)这句只是修改内存中XML节点的值,所以还要建立DOMSource对象和StreamResult对象,将内存中的XML
   信息写回文件。

TestSCXML.java基本没有什么变化,就在最后增加了测试setTextValue的代码:

SCXMLUtils.setTextValue(xmldoc, node1.getFirstChild(), "你好");

现在运行TestSCXML看看XML是否被修改了,

posted on 2006-06-19 22:55 胖娃娃 阅读(430) 评论(0)  编辑  收藏 所属分类: XML

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


网站导航: