改成这样吧,简单些:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
   "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<Config>
<DBServerIP>192.168.0.5</DBServerIP>
<DBName>server</DBName>
<DBUserName>sa</DBUserName>
<DBPassWord>ttak</DBPassWord>
</Config>
我写的程序如下:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File xmlfile = new File("config/MMSConfig.xml");
Document doc = builder.parse(xmlfile);
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();System.out.println(root.getNodeName());
for (int i = 0; i < children.getLength(); i++)
{
  Node child = children.item(i);
  System.out.println(child.getNodeName());
  if (child instanceof Element){
     Element childElement = (Element) children;
     String nodeValue = child.getNodeValue();
     System.out.println(nodeValue);
     if (child.getNodeName().equals("DBServerIP"))
         DBServerIP = nodeValue;
     else if (child.getNodeName().equals("DBName"))
DBName = nodeValue;
     else if (child.getNodeName().equals("DBUserName"))
         DBUserName = nodeValue;
     else if (childElement.getTagName().equals("DBPassWord"))
DBPassWord = nodeValue;
  }
}
nodeValue的值总是null,不知道是为什么?

解决方案 »

  1.   

    你把getNodeValue换成getText()。×××××××××××××××××
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    File xmlfile = new File("config/MMSConfig.xml");
    Document doc = builder.parse(xmlfile);
    Element root = doc.getRootElement();DBServerIP=root.getChild("DBServerIP").getText();
    其他的属性也这样赋值。
      

  2.   

    是childElement.getNodeValue()?
      

  3.   

    我也想借楼主的地方问一个问题,我的Element 的对象怎么没有getChild的方法,就像上面的程序里的DBServerIP=root.getChild("DBServerIP").getText();
    我用的事j2sdk1.4.2Element 好像继承的NODE类。我查了j2sdk1.4.2也没有看到有getChild的方法,不知道为什么?????
      

  4.   

    我知道了应该是: child.getFirstChild().getNodeValue()
      

  5.   

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC
       "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
       "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
    <Config>
    <DBServerIP>192.168.0.5</DBServerIP>
    <DBName>server</DBName>
    <DBUserName>sa</DBUserName>
    <DBPassWord>ttak</DBPassWord>
    </Config>用jdom 读写的:)import org.jdom.*;
    import org.jdom.input.*;
    import java.util.*;
    import java.io.*;
    public class writexml{
       public static void main (String[] args) throws Exception //如果有任何异常抛出
    {
      SAXBuilder sb = new SAXBuilder();
      Document doc = sb.build( new FileInputStream("/config/config.xml"));//读入config.xml文件
      Element root = doc.getRootElement(); //取得根结点就是例子中的<config>
      List list = root.getChildren();
      for (int i=0; i<list.size(); i++)
      {
    System.out.println("--------------")
    Element dbname = list.getchild("DBServerIP");
    String servname = dbname.getText();
    System.out.println("dbname->" + servname);   
      }

    }
    }