利用DOM解析器XML文件

        DOM是Document Object Model即文档对象模型的缩写,是一种与浏览器、平台、语言的接口,已经被W3C标准化。Java的DOM解析器负责解析xml文件。它容易理解,根据XML文件生成一个树结构,所以如果该xml文件很大,则会消耗很大的内存。

解析xml文件,首先需要创建DocumentBuilderFactory实例:
DocumentBuilderFactory factory = DocumentBuilderFacory.newInstance();
接下来创建DocumentBuilder实例:
DocumentBuilder builder = factory.newDocumentBuilder();
下面就可以导入xml文件了。DocumentBuilder支持File、URL和InputStream三种格式的参数:
Document doc = builder.parse(file);
Document doc = builder.parse(url);
Document doc = builder.parse(inputStream);
 
Document就是xml文档的树型结构在内存中的表现。它由实现Node接口及其多个子接口的类对象构成。
Element root = doc.getDocumentElement();
root就是xml的根元素(元素是xml文件中以“<”和“>”包裹起来的内容),通过root可以取得其元素名、属性名及值、文本内容等。
 
下面是一个解析xml文件的完整例子:
     /**
     * 解析XML文件
     * 
     * 
@param file
     *             XML文件
     
*/

    
public   void  parseXML(File file)  {
        
try{
            DocumentBuilderFactory factory 
= DocumentBuilderFactory.newInstance();
            factory.setValidating(
true);
            factory.setIgnoringElementContentWhitespace(
true);
            DocumentBuilder builder 
= factory.newDocumentBuilder();
            Document doc 
= builder.parse(file);
            Element root 
= doc.getDocumentElement();    //取得根元素
            parseElement(root);
        }
 catch(IOException e){
            JOptionPane.showMessageDialog(
this, e);
        }
 catch(ParserConfigurationException e){
            JOptionPane.showMessageDialog(
this, e);
        }
 catch(SAXException e){
            JOptionPane.showMessageDialog(
this, e);
        }

    }

    
    
/**
     * 解析元素
     * 
     * 
@param e
     *             元素
     
*/

    
public   void  parseElement(Element e)  {
        
if(e != null{
            System.out.println(
"元素名:" + e.getTagName());
            NamedNodeMap attributes 
= e.getAttributes();    //取得当前元素的属性列表
            for(int i=0; i<attributes.getLength(); i++){
                Node node 
= attributes.item(i);
                
if(node != null{
                    System.out.println(
"属性:" + node.getNodeName() + "=" + node.getNodeValue());
                }

            }

            
            NodeList list 
= e.getChildNodes();   //取得当前元素的所有子节点
            if(list != null{
                
for(int i=0; i<list.getLength(); i++{
                    Node node 
= list.item(i);
                    
if(node != null){
                        
if(node instanceof Element)    //子元素
                            parseElement((Element)node);
                        
else if(node instanceof Text)    //该元素的文本
                            System.out.println("文本:" + ((Text)node).getData());
                    }
;
                }

            }

        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值