JAVA读取xml文件

一  dom读取方式

  1. package com;  
  2.  
  3. import org.w3c.dom.*; 
  4. import javax.xml.parsers.*; 
  5. import java.io.*; 
  6.  
  7. public class Parse{ 
  8. //Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对  
  9. //内存的操作来实现对XML的操作,首先第一步获取XML相关的Document  
  10.   private Document doc= null
  11.    
  12. public void init(String xmlFile) throws Exception{ 
  13.   //很明显该类是一个单例,先获取产生DocumentBuilder工厂  
  14.   //的工厂,在通过这个工厂产生一个DocumentBuilder,  
  15.   //DocumentBuilder就是用来产生Document的  
  16.   DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 
  17.   DocumentBuilder db=dbf.newDocumentBuilder();   
  18.   //这个Document就是一个XML文件在内存中的镜像  
  19.   doc=db.parse( new File(xmlFile)); 
  20. //该方法负责把XML文件的内容显示出来  
  21. public void viewXML(String xmlFile) throws Exception{ 
  22.   this .init(xmlFile); 
  23.   //在xml文件里,只有一个根元素,先把根元素拿出来看看  
  24.   Element element=doc.getDocumentElement(); 
  25.   System.out.println( "根元素为:" +element.getTagName()); 
  26.    
  27.   NodeList nodeList=doc.getElementsByTagName( "dbstore" ); 
  28.   System.out.println( "dbstore节点链的长度:" +nodeList.getLength()); 
  29.    
  30.   Node fatherNode=nodeList.item( 0 ); 
  31.   System.out.println( "父节点为:" +fatherNode.getNodeName()); 
  32.    
  33.   //把父节点的属性拿出来  
  34.   NamedNodeMap attributes=fatherNode.getAttributes(); 
  35.    
  36.   for ( int i= 0 ;i<attributes.getLength();i++){ 
  37.    Node attribute=attributes.item(i); 
  38.    System.out.println( "dbstore的属性名为:" +attribute.getNodeName()+ " 相对应的属性值为:" +attribute.getNodeValue()); 
  39.   } 
  40.  
  41.   NodeList childNodes = fatherNode.getChildNodes(); 
  42.   System.out.println(childNodes.getLength()); 
  43.   for ( int j= 0 ;j<childNodes.getLength();j++){ 
  44.    Node childNode=childNodes.item(j); 
  45.    //如果这个节点属于Element ,再进行取值  
  46.    if (childNode instanceof Element){ 
  47.     //System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());  
  48.     System.out.println( "子节点名为:" +childNode.getNodeName()+ "相对应的值为" +childNode.getFirstChild().getNodeValue()); 
  49.    } 
  50.   } 
  51.    
  52.   
  53. public static void main(String[] args) throws Exception{ 
  54.   Parse parse= new Parse(); 
  55.  
  56. //具体的XML文件  
  57.   parse.viewXML( "netct.xml" ); 
  58. }  
  59.   
  60. 二 dom4j读取
  61. import java.io.*;
    import java.util.*;
    import org.dom4j.*;
    import org.dom4j.io.*;

    public class MyXMLReader2DOM4J {
    public static void main(String arge[]) {
          long lasting = System.currentTimeMillis();
              try {
                   File f = new File("data_10k.xml");
                   SAXReader reader = new SAXReader();
                   Document doc = reader.read(f);
                   Element root = doc.getRootElement();
                   Element foo;
                   for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {
                   foo = (Element) i.next();
                    System.out.print("车牌号码:" + foo.elementText("NO"));
                    System.out.println("车主地址:" + foo.elementText("ADDR"));
                   }
                  } catch (Exception e) {
                           e.printStackTrace();
                  }
             }
        }

        记得还要把jaxen.jar文件添加要路径下 只有dom4j,jar是会报错 org.jaxexexception大概就是这样的异常!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值