胖娃娃

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

  今天学习了Java读取XML的简单方法,主要用到了javax.xml.pasers.*中的一些对象(注:可能会用到的包,xercesImpl.jar、xml-apis.jar
jdom.jar
),首先构造一个简单的操作XML的工具类(Simple Control XML)SCXMLUtils,具体代码如下(SCXMLUtils.java):

package  SCXML;

import  java.io.FileWriter;

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

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 ;

        }


    }


}

说明:其中getXMLDoc用于返回一个指定xml文件的Document对象,getChild返回一个指定节点的下一层中某一个指定名称的节点对象,
   getTextValue用于返回一个指定节点内的文本内容。

用来测试这个工具类的TestSCXML.java:

import  org.w3c.dom.Document;
import  org.w3c.dom.Element;
import  org.w3c.dom.Node;
import  SCXML. * ;

public   class  TestSCXML  {

    
public   static   void  main(String[] args)  {

        String nodevalue 
=   null ;
        Document xmldoc 
=  SCXMLUtils.getXMLDoc( " test.xml " );
        
if  (xmldoc  ==   null {

            System.out.println(
" Create XML Document failed! " );
            
return ;

        }

        Element root 
=  xmldoc.getDocumentElement();
        
if  (root  ==   null {

            System.out.println(
" Get root of XML Document failed! " );
            
return ;

        }

        Node node1 
=  SCXMLUtils.getChild(root,  " Master " );
        
if  (node1  ==   null {

            System.out.println(
" Get node failed! " );
            
return ;

        }

        nodevalue 
=  SCXMLUtils.getTextValue(node1.getFirstChild());
        
if  (nodevalue  ==   null {

            System.out.println(
" Get value of node failed! " );
            
return ;

        }

        System.out.println(
" The Master is  "   +  nodevalue.trim());

    }


}


说明:首先调用getXMLDoc获得test.xml的Document对象,然后调用getDocumentElement()函数获得这个xml文件的根节点(即Home节点),然后
   执行getChild(root, "Master")返回根节点下一层中,名为Master的子节点对象node1,然后的执行getTextValue(node1.getFirstChild())获得Master
   节点中的文本信息,这里特别说明一下,为什么是getTextValue(node1.getFirstChild())而不是getTextValue(node1),因为<Master>...</Master>
   之间的内容也被认为是一个节点(也可以说成是一个text对象),并且这个节点是Master节点下的第一个子节点,所以用getFirstChild()
   来获得Master节点中的文本内容。

测试用的test.xml:
<?xml version="1.0" encoding="GBK"?>
<Home>
    <Master>fatcatman</Master>
</Home>

运行结果:
The Master is fatcatman

今天学习了如何Java读取XML某个节点中的内容,明天继续学习修改XML的方法。
posted on 2006-06-18 18:52 胖娃娃 阅读(617) 评论(0)  编辑  收藏 所属分类: XML

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


网站导航: