xml文件内容:
<database>
<localHost>myLocalHost</localHost>
<databaseName>myDatabaseName</databaseName>
</database>程序:import java.sql.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class readDatabase {
//public Connection conn;
//public Statement stmt;
public  static String  localHost="33";
public static  String db_name="";public void readDataBase()
{

}public static void readDatabase1(){
DocumentBuilderFactory theDocumentBuilderFactory=DocumentBuilderFactory.newInstance();
String path = "D:\\hy\\eclipse\\AddressBookSpace\\AddressBook\\WEB-INF";
path=path+"\\database.xml";
try{
DocumentBuilder theDocumentBuilder=theDocumentBuilderFactory.newDocumentBuilder();
Document theDocument=theDocumentBuilder.parse(path);
Element theParentElement = theDocument.getDocumentElement();
NodeList theNodeList = theParentElement.getElementsByTagName("database");for(int i=0;i<theNodeList.getLength();i++){Element theChildElement =(Element)theNodeList.item(i);
NodeList theChildNodeList=null;
//String outString="";theChildNodeList=theChildElement.getElementsByTagName("localHost");
localHost=(theChildNodeList.item(0)).getFirstChild().getNodeValue();
theChildNodeList=theChildElement.getElementsByTagName("databaseName");
db_name=(theChildNodeList.item(0)).getFirstChild().getNodeValue();
}
}
catch(Exception e){
e.printStackTrace();
}}
public static void main(String args[]){
readDatabase1();
System.out.println("ip="+localHost);
System.out.println("db_name="+db_name);}}
我执行了一下怎莫出来是空值。另外eclipse调试怎莫单步执行,好像没有.net方便啊。高手指点一下啊

解决方案 »

  1.   

    Window→Open perspective→Debug
    打开之后你就知道了,具体调试方法跟其他IDE大同小异
      

  2.   

    谢谢楼上,只是没有像.net中的即时窗口感觉方便。
    另外,高手们能帮我看看程序的问题吗,我跟踪了一下theNodeList.getLength();长度为0,就是说没有数据读进来。韦什莫呢
      

  3.   

    我没用过.Net的调试,不太好比较,但感觉Eclipse的调试功能还算不错。楼主可能对Eclipse不熟悉,用多了习惯了就好了。至于程序,我测试测试看看。
      

  4.   

    搞定了,看以下代码:import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;public class ReadDatabase {
        public static String localHost = "33";
        public static String db_name = "";    public static void readDatabase1() {        DocumentBuilderFactory theDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
            String path = "D:\\hy\\eclipse\\AddressBookSpace\\AddressBook\\WEB-INF\\database.xml";
            try {
                DocumentBuilder theDocumentBuilder = theDocumentBuilderFactory.newDocumentBuilder();
                Document theDocument = theDocumentBuilder.parse(path);
                
                NodeList localHostList = theDocument.getElementsByTagName("localHost");
                if (localHostList.getLength() > 0) {
                    localHost = (localHostList.item(0)).getFirstChild().getNodeValue();
                }
                
                NodeList databaseNameList = theDocument.getElementsByTagName("databaseName");
                if (databaseNameList.getLength() > 0) {
                    db_name = (databaseNameList.item(0)).getFirstChild().getNodeValue();
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }    }    public static void main(String args[]) {
            readDatabase1();
            System.out.println("ip=" + localHost);
            System.out.println("db_name=" + db_name);
        }}
      

  5.   

    多谢楼上,刚才搞了半天,也找到了这个方法,但是
    (localHostList.item(0)).getFirstChild().getNodeValue();能取得值,
    (localHostList.item(0)).getFirstChild().getNodeName()就不能取得节点的名称呢。郁闷
      

  6.   

    <root>   
            <server>         
                <ServerName>server</ServerName>        
            </server>   
            <database>   
                <DatabaseName>myDatabase</DatabaseName>  
        <UserName>myUserName</UserName> 
        <Password>myPassword</Password>
            </database>   
      </root> 
    我现在把xml变成这样了,调了半天也没整好 。getNodeName()就得到#text,这是为什莫阿
      

  7.   

    因为我要得到的事节点的名称,根据节点名得到节点值,节点的值有可能会变,所以不能用getNodeValue();
      

  8.   

    按照你的需求,我又把代码重新写了一遍。
    要注意的是,取得结点名称不是(localHostList.item(0)).getFirstChild().getNodeName()而是(localHostList.item(0)).getNodeName()
    代码如下:import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;public class ReadDatabase {
        public static String serverName;
        public static String databaseName;
        public static String userName;
        public static String password;    public static void readDatabase1() {        DocumentBuilderFactory theDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
            String path = "D:\\database.xml";
            try {
                DocumentBuilder theDocumentBuilder = theDocumentBuilderFactory.newDocumentBuilder();
                Document theDocument = theDocumentBuilder.parse(path);
                theDocument.getDocumentElement();
                
                NodeList nodelist;
                
                nodelist = theDocument.getElementsByTagName("ServerName");
                if (nodelist.getLength() > 0) {
                    serverName = (nodelist.item(0)).getFirstChild().getNodeValue();
                    System.out.println(nodelist.item(0).getNodeName() + "=" + serverName);
                }
                
                nodelist = theDocument.getElementsByTagName("DatabaseName");
                if (nodelist.getLength() > 0) {
                    databaseName = (nodelist.item(0)).getFirstChild().getNodeValue();
                    System.out.println(nodelist.item(0).getNodeName() + "=" + databaseName);
                }
                
                nodelist = theDocument.getElementsByTagName("UserName");
                if (nodelist.getLength() > 0) {
                    userName = (nodelist.item(0)).getFirstChild().getNodeValue();
                    System.out.println(nodelist.item(0).getNodeName() + "=" + userName);
                }
                
                nodelist = theDocument.getElementsByTagName("Password");
                if (nodelist.getLength() > 0) {
                    password = (nodelist.item(0)).getFirstChild().getNodeValue();
                    System.out.println(nodelist.item(0).getNodeName() + "=" + password);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }    }    public static void main(String args[]) {
            readDatabase1();
        }}
      

  9.   

    以上代码的输出结果是:
    ServerName=server
    DatabaseName=myDatabase
    UserName=myUserName
    Password=myPassword
      

  10.   

    多谢初晨之阳。刚才自己也作了一下。import   javax.xml.transform.*;   
    import   javax.xml.transform.dom.DOMSource;   
    import   javax.xml.transform.stream.StreamResult; 
    import   javax.xml.parsers.*;import   org.w3c.dom.*; public class readDatabaseXML {
    public readDatabaseXML(){}
    public static String ServerName=null;
    public static String DatabaseName=null;
    public static String FilePath=null;
    public static String UserName=null;
    public static String Password=null;

    public void readXML()
    {
    try{
     
    DocumentBuilderFactory   factory   =   DocumentBuilderFactory.newInstance();   
    DocumentBuilder   builder=factory.newDocumentBuilder();  
    String filePaht=FilePath;
    Document   doc=builder.parse(filePaht);   
    doc.normalize();   
      
    NodeList   books   =doc.getDocumentElement().getChildNodes();   
    Node   book;   
    NodeList   lists;   

    book=books.item(1); 
    // 浏览查看   
    for(int   i=1;i<books.getLength();i++)   
    {   
    book=books.item(i); 
                               //下面两句测试用
    String sssss=books.item(i).toString();
    System.out.println(sssss);
    lists=book.getChildNodes();  
    for(int   j=1;j<lists.getLength();j++)   
    {   
    if(lists.item(j).getNodeType()==Node.ELEMENT_NODE)
    {
    if(((Element)lists.item(j)).getNodeName().equals("ServerName"))
    {
    ServerName=((Element)lists.item(j)).getTextContent().toString();
    }else if(((Element)lists.item(j)).getNodeName().equals("DatabaseName"))
    {
    DatabaseName=((Element)lists.item(j)).getTextContent().toString();
    }else if(((Element)lists.item(j)).getNodeName().equals("UserName"))
    {
    UserName=((Element)lists.item(j)).getTextContent().toString();
    }else if(((Element)lists.item(j)).getNodeName().equals("Password"))
    {
    Password=((Element)lists.item(j)).getTextContent().toString();
    }
    }
    }   
    }    
    }
    catch(Exception e)
    {System.out.println(e);}
    }}这句话
    System.out.println(sssss);
    打出这样的结果:
    [server: null]
    [#text:    
            ]
    [database: null]
    [#text:    
      ]
    这是为什莫呢,我的xml是用文本编辑器写的,直接改了个后缀
      

  11.   

    感觉用初晨之阳你的方法更简单一些。能不能帮我解释一下下面的结果为什莫会出现呢,多谢了
    [server: null]
    [#text:    
            ]
    [database: null]
    [#text:    
      ]
      

  12.   

    你的代码基本上是对的,只有一点小问题,我帮你改了。代码如下:import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;public class readDatabaseXML {    public static String ServerName = null;
        public static String DatabaseName = null;
        public static String FilePath = null;
        public static String UserName = null;
        public static String Password = null;    public void readXML() {
            try {            DocumentBuilderFactory factory = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                String filePath = "D:\\database.xml";
                Document doc = builder.parse(filePath);
                doc.normalize();            NodeList books = doc.getDocumentElement().getChildNodes();
                Node book;
                NodeList lists;            // 浏览查看
                for (int i = 0; i < books.getLength(); i++) {
                    book = books.item(i);
                    lists = book.getChildNodes();
                    for (int j = 1; j < lists.getLength(); j++) {
                        if (lists.item(j).getNodeType() == Node.ELEMENT_NODE) {
                            if (((Element) lists.item(j)).getNodeName().equals(
                                    "ServerName")) {
                                ServerName = ((Element) lists.item(j))
                                        .getTextContent().toString();
                                System.out.println("ServerName=" + ServerName);
                            } else if (((Element) lists.item(j)).getNodeName()
                                    .equals("DatabaseName")) {
                                DatabaseName = ((Element) lists.item(j))
                                        .getTextContent().toString();
                                System.out.println("DatabaseName=" + DatabaseName);
                            } else if (((Element) lists.item(j)).getNodeName()
                                    .equals("UserName")) {
                                UserName = ((Element) lists.item(j))
                                        .getTextContent().toString();
                                System.out.println("UserName=" + UserName);
                            } else if (((Element) lists.item(j)).getNodeName()
                                    .equals("Password")) {
                                Password = ((Element) lists.item(j))
                                        .getTextContent().toString();
                                System.out.println("Password=" + Password);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }}程序输出为:ServerName=server
    DatabaseName=myDatabase
    UserName=myUserName
    Password=myPassword
      

  13.   

    多谢初晨之阳的帮助。我不太明白的是这句,
    NodeList books = doc.getDocumentElement().getChildNodes();
    取得了子节点,但我用循环打出来
    book=books.item(i); 
    String sssss=books.item(i).toString();
    System.out.println(sssss);
    得到了这样的结果,是为什莫呢。能指点我一下吗
    [#text: 
    ]
    [server: null]
    [#text: 
    ]
    [database: null]
    [#text: 
    ]
      

  14.   

    String sssss=books.item(i).toString();
    System.out.println(sssss);
    你这样输出的只是books.item(i).toString(),而具体输出格式是由toString()方法决定的,并不一定能输出你想的ServerName等值。book = books.item(i);
    lists = book.getChildNodes();
    ServerName = ((Element) lists.item(j)).getTextContent().toString();
    System.out.println("ServerName=" + ServerName);
    这里的输出才是真正的输出ServerName的值。是books.item(i)再调用getChildNodes()得到list,然后再调用list每个元素的getTextContent()才能得到你想要的值。差别就在这里,前者相当于输出books.item(i),后者相当于输出books.item(i).getChildNodes().getTextContent()