<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"><!-- com.microsoft.sqlserver.jdbc.SQLServerDriver -->
</property>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/bims"><!-- jdbc:sqlserver://127.0.0.1:1433;databaseName=wootion -->
</property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
</beans>
我要读取url username password Java读取该怎么写 自己写的太繁琐 大家指点下

解决方案 »

  1.   

    不是又现成的读xml的方法吗?dom4j,jdom,
    干嘛要自己费劲读啊?
      

  2.   

    http://zwm.javaeye.com/blog/57060
    看看这个吧应该适合!http://edu.codepub.com/2010/0108/19503.php
      

  3.   

    http://read.newbooks.com.cn/info/60670.html
      

  4.   

    你是要解析那个spring配置文件吗?
    你先根据XML结构定义好对应的bean, 然后用DOM4J 或JDOM 或 ......  ,有很多工具的。 easy啦。
    不会的话,网上一搜一大把的
      

  5.   

    这是spring的配置文件 注入后可自己读取
      

  6.   


            File file = new File("C:\\test.xml");
            SAXReader reader = new SAXReader();
            Document doc = reader.read(file);
            
            Map xmlMap = new HashMap();
            xmlMap.put("nSpace", "http://www.springframework.org/schema/beans");
            XPath x = doc.createXPath("//nSpace:property");
            x.setNamespaceURIs(xmlMap);
            List list = x.selectNodes(doc);        for(int i = 0; i < list.size(); i++){
                Element e = (Element)list.get(i);
                System.out.println(e.attributeValue("name") + " : " + e.attributeValue("value"));
            }结果:
    driverClassName : com.mysql.jdbc.Driver
    url : jdbc:mysql://127.0.0.1:3306/bims
    username : root
    password : 123比较麻烦的地方是名字空间的处理
      

  7.   

    上面的写麻烦了,这个。
           Node node = doc.selectSingleNode("//*[@name='url']");
           Element e = (Element)node;
           System.out.println(e.attributeValue("name") + " : " + e.attributeValue("value"));
           
           node = doc.selectSingleNode("//*[@name='username']");
           e = (Element)node;
           System.out.println(e.attributeValue("name") + " : " + e.attributeValue("value"));       node = doc.selectSingleNode("//*[@name='password']");
           e = (Element)node;
           System.out.println(e.attributeValue("name") + " : " + e.attributeValue("value"));