呵呵,我的写法和你不一样,代码如下,使用很正常的
配置文件如下:
<?xml version="1.0" encoding="GB2312"?>
<config>
<weburl>http://172.16.2.118/gstest.asp</weburl>
<jodbc>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=GSTEST</jodbc>
<uid>sa</uid>
<pwd />
</config>部分代码
package voicexml;
import java.util.*;
import java.text.*;
import java.net.*;
import javax.swing.*;
import java.io.*;
import java.net.HttpURLConnection;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.XmlDocument;
import org.xml.sax.*;
import org.w3c.dom.Text;
//*********************读取配置文件**********************
public String GetWebURL(JTextArea weburl,JTextArea jdbc,JTextArea sa,JPasswordField pwd)
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=null;
try {
db = dbf.newDocumentBuilder();
}
catch (ParserConfigurationException ex) {JOptionPane.showMessageDialog(null,"获取WEBURL地址失败,请检测文件是否存在");
return "";
}
Document doc=null;
try {
//linux下的当前路径,windows下可以改成 doc = db.parse("config.xml");
doc = db.parse("./config.xml"); }
catch (IOException ex1) {JOptionPane.showMessageDialog(null,"获取WEBURL地址失败,请检测文件是否存在");
return "";
}
catch (SAXException ex1) {
}
String textvalue="";
Element root=doc.getDocumentElement();
NodeList nodelist=root.getElementsByTagName("weburl");
Element e=(Element)nodelist.item(0); Text t=(Text) e.getFirstChild();
if(t!=null)
textvalue=t.getNodeValue().trim();
if(weburl!=null)
weburl.setText(textvalue);
NodeList nodelist1=root.getElementsByTagName("jodbc");
Element e1=(Element)nodelist1.item(0);
Text t1=(Text) e1.getFirstChild();
if(t1!=null && jdbc!=null)
jdbc.setText(t1.getNodeValue().trim()); NodeList nodelist2=root.getElementsByTagName("uid");
Element e2=(Element)nodelist2.item(0);
Text t2=(Text) e2.getFirstChild();
if(t2!=null && sa!=null)
sa.setText(t2.getNodeValue().trim()); NodeList nodelist3=root.getElementsByTagName("pwd");
Element e3=(Element)nodelist3.item(0);
Text t3=(Text) e3.getFirstChild();
if(t3!=null && pwd!=null)
pwd.setText(t3.getNodeValue().trim()); //String textvalue=t.getNodeValue();
return textvalue; }象你的那个配置文件只要在这里Element e=(Element)nodelist.item(0);做个循环不用item(0)就行了
配置文件如下:
<?xml version="1.0" encoding="GB2312"?>
<config>
<weburl>http://172.16.2.118/gstest.asp</weburl>
<jodbc>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=GSTEST</jodbc>
<uid>sa</uid>
<pwd />
</config>部分代码
package voicexml;
import java.util.*;
import java.text.*;
import java.net.*;
import javax.swing.*;
import java.io.*;
import java.net.HttpURLConnection;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.XmlDocument;
import org.xml.sax.*;
import org.w3c.dom.Text;
//*********************读取配置文件**********************
public String GetWebURL(JTextArea weburl,JTextArea jdbc,JTextArea sa,JPasswordField pwd)
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=null;
try {
db = dbf.newDocumentBuilder();
}
catch (ParserConfigurationException ex) {JOptionPane.showMessageDialog(null,"获取WEBURL地址失败,请检测文件是否存在");
return "";
}
Document doc=null;
try {
//linux下的当前路径,windows下可以改成 doc = db.parse("config.xml");
doc = db.parse("./config.xml"); }
catch (IOException ex1) {JOptionPane.showMessageDialog(null,"获取WEBURL地址失败,请检测文件是否存在");
return "";
}
catch (SAXException ex1) {
}
String textvalue="";
Element root=doc.getDocumentElement();
NodeList nodelist=root.getElementsByTagName("weburl");
Element e=(Element)nodelist.item(0); Text t=(Text) e.getFirstChild();
if(t!=null)
textvalue=t.getNodeValue().trim();
if(weburl!=null)
weburl.setText(textvalue);
NodeList nodelist1=root.getElementsByTagName("jodbc");
Element e1=(Element)nodelist1.item(0);
Text t1=(Text) e1.getFirstChild();
if(t1!=null && jdbc!=null)
jdbc.setText(t1.getNodeValue().trim()); NodeList nodelist2=root.getElementsByTagName("uid");
Element e2=(Element)nodelist2.item(0);
Text t2=(Text) e2.getFirstChild();
if(t2!=null && sa!=null)
sa.setText(t2.getNodeValue().trim()); NodeList nodelist3=root.getElementsByTagName("pwd");
Element e3=(Element)nodelist3.item(0);
Text t3=(Text) e3.getFirstChild();
if(t3!=null && pwd!=null)
pwd.setText(t3.getNodeValue().trim()); //String textvalue=t.getNodeValue();
return textvalue; }象你的那个配置文件只要在这里Element e=(Element)nodelist.item(0);做个循环不用item(0)就行了
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货