读取XML文件

方式1:

package jp.co.Cheng;
 
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

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

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
public   class   XmlPropertyReader   {  
        private   static   Map<String,String>   propertyMap   =   null;  
        private   static   final   String   fileName   =   "dbProperties.xml";       
 
        public   static   String   getProperty(String   key)   throws   Exception   {  
                if   (propertyMap   ==   null)   {  
                        propertyMap   =   loadProperties();  
                }  
 
                if   (propertyMap.containsKey(key))   {  
                        return   (String)   propertyMap.get(key);  
                }   else   {  
                        throw   new   Exception("Property   "   +   key   +   "   does   not   exist");  
                }  
        }  
 
        private   static   Map<String,String>   loadProperties()   throws   Exception   {  
                Document   doc   =   null;  
 
                try   {  
                        DocumentBuilder   builder   =   DocumentBuilderFactory.newInstance()  
                                                                            .newDocumentBuilder();  
 
                        InputStream   is   =   XmlPropertyReader.class.getResourceAsStream(  
                                        fileName);  
                        doc   =   builder.parse(is);  
                        //ServletUtility.printXML(doc);  
 
                        return   getMap(doc.getElementsByTagName("property"));  
 
                }   catch   (Exception   ex)   {
                  ex.printStackTrace();
                        throw   ex;  
                        //logger.error(ex);  
                }  
        }  
 
        private   static   Map<String,String>   getMap(NodeList   list)   throws   Exception   {  
                Map<String,String>   map   =   new   HashMap<String,String>();  
 
                if   (list   ==   null)   {  
                        return   map;  
                }  
 
                for   (int   i   =   0;   i   <   list.getLength();   i++)   {  
                        String   key   =   null;  
                        String   value   =   null;  
                        Node   parentNode   =   list.item(i);
                       
                        NamedNodeMap   attributes   =   parentNode.getAttributes();
                        Node   att   =   attributes.item(0);
                        if (att.getNodeName().equals("name")){
                         key = att.getNodeValue();
                         Node childNode = parentNode.getFirstChild();
                         value = childNode.getNodeValue();
                        }
 
//                        NamedNodeMap   attributes   =   parentNode.getAttributes();  
 
//                        for   (int   j   =   0;   j   <   attributes.getLength();   j++)   {  
//                                Node   att   =   attributes.item(j);  
// 
//                                if   (att.getNodeName().equals("name"))   {  
//                                        key   =   att.getNodeValue();
//                                        Node childNode = att.getFirstChild();
//                                       
//                                }   else   if   (att.getNodeName().equals("value"))   {  
//                                        value   =   att.getNodeValue();  
//                                }   else   {  
//                                        throw   new   Exception(  
//                                                        "Attribute   must   be   either   key   or   value");  
//                                }  
//                        }  
 
                        if   ((key   ==   null)   ||   (value   ==   null))   {  
                                throw   new   Exception("Either   key   or   value   is   not   present");  
                        }  
 
                        map.put(key,   value);  
                }  
 
                return   map;  
        }  
}

 

xml样式:

<?xml version='1.0' encoding="UTF-8"?>
   
  <hibernate-configuration>  
   
  <session-factory>  
  <property   name="connection.username">jtuser</property>  
  <property   name="connection.url">jdbc:microsoft:sqlserver://172.44.22.43:1433</property>  
  <property   name="dialect">org.hibernate.dialect.SQLServerDialect</property>  
  <property   name="myeclipse.connection.profile">172.22.11.43</property>  
  <property   name="connection.password">jt123</property>  
  <property   name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>  
  </session-factory>  
   
  </hibernate-configuration>

 

方式2:

public class LoadXml ...{

    /** *//**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception ...{
        // TODO Auto-generated method stub
        //从 XML 文档获取 DOM 文档实例。使用此类,应用程序员可以从 XML 获取一个Document
        DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
        FileInputStream in=new FileInputStream("F:/wrokspace/learnspring/src/ch7/111.xml");
        Document document=db.parse(in);
        NodeList nl=document.getElementsByTagName("a");
        showElem(nl);       
        in.close();
    }
    /** *//**
     * 通过递归得到所有子节点
     * @param nl
     */
    public static void showElem(NodeList nl)...{
        for(int i=0;i<nl.getLength();i++)...{
            Node n=nl.item(i);
            if(n.hasChildNodes())...{
                System.out.print("<"+n.getNodeName()+">");
                //递归
                showElem(n.getChildNodes());
                System.out.print("</"+n.getNodeName()+">");
            }else...{
                //判断是不是文本
                if(n.getNodeType()==Node.TEXT_NODE)...{
                    System.out.print(n.getNodeValue());
                }else...{
                    System.out.print("<"+n.getNodeName()+">");
                    System.out.print("</"+n.getNodeName()+">");
                }
                break;
            }
        }
    }

}
xml文档的格式:

<aa><a><t1><tt1>32324</tt1></t1><c1>cccc</c1></a><a><t1><tt1></tt1></t1><c1>cccc</c1></a></aa

文章出处:http://www.diybl.com/course/3_program/java/javashl/2008520/117123.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值