如何读取xml配置文件|java如何读取XML配置文件

⑴ 如何从XML文件里面读取配置文件

类public string GetConStr(){try{XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.MapPath("App_Data\\config.xml"));XmlNode node1 = xmlDoc.SelectSingleNode("configuration/ConnectionStrings1/DataSource");XmlNode node2 = xmlDoc.SelectSingleNode("configuration/ConnectionStrings1/uid");XmlNode node3 = xmlDoc.SelectSingleNode("configuration/ConnectionStrings1/pwd");string strcon = "Data Source=" + node1.InnerText + ";uid=" + node2.InnerText + ";pwd=" + node3.InnerText;return strcon;}catch{Response.Redirect("Config.aspx");return null;}}调用IConfig iconfig = new IConfig();iconfig .GetConStr();

⑵ 易语言读写XML配置文件

用支持库的话 比较麻烦 , 最简单的, 就是改后缀, 读入文件 写到文件, 再改回名称就好了

⑶ java如何读取XML配置文件

JAVA与XML文件,可以说是软件开发的“黄金搭档”,而如何使用JAVA完成对文件的读取,是我们首先要解决的问题。一、XML文件这个示例文件包括了用来打开ORACLE数据库的各种参数<?xml version="1.0" encoding="UTF-8"?><dbmsg> <dbinfo> <drivername>oracle.jdbc.driver.OracleDriver</drivername> <sConnStr>jdbc:oracle:thin:@11.88.225.80:1521:VOUCHERDB</sConnStr> <username>SYS AS SYSDBA</username> <password>voucherdb</password> </dbinfo></dbmsg>二、编写类名为ReadXml的类,用于解析XML文件我们要在应用程序中打开数据库,就必须完成对该文件中drivername、sConnStr、username、password的读取,通过查找有关资料,笔者编制了以下程序,用于读取文件名为filename的XML文件。package voucher.basic;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;public class ReadXml { private String drivername; private String sConnStr; private String username; private String password; public String getDrivername() { return drivername; }public String getSConnStr() { return sConnStr; }public String getUsername() { return username; }public String getPassword() { return password; } public void setDrivername(String drivername) { this.drivername = drivername; }public void setSConnStr(String connStr) { sConnStr = connStr; }public void setUsername(String username) { this.username = username; }public void setPassword(String password) { this.password = password; }public ReadXml(String fileName){ DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); try { DocumentBuilder dombuilder=domfac.newDocumentBuilder(); InputStream is=new FileInputStream(fileName); Document doc=dombuilder.parse(is); Element root=doc.getDocumentElement(); NodeList dbinfo=root.getChildNodes(); if(dbinfo!=null){ for(int i=0;i<dbinfo.getLength();i++){ Node db=dbinfo.item(i); for(Node node=db.getFirstChild();node!=null;node=node.getNextSibling()){ if(node.getNodeType()==Node.ELEMENT_NODE){ if(node.getNodeName().equals("drivername")){ setDrivername(node.getFirstChild().getNodeValue()); } if(node.getNodeName().equals("sConnStr")){ setSConnStr(node.getFirstChild().getNodeValue()); } if(node.getNodeName().equals("username")){ setUsername(node.getFirstChild().getNodeValue()); } if(node.getNodeName().equals("password")){ setPassword(node.getFirstChild().getNodeValue()); } } } } } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }这个以ReadXml命名的类,使用了参数为文件名的构造方法,用户只要将配置文件我名称传递给该方法,就可以完成对XML文件的解析,进而完成对相应参数数的读取。三、如何获取XML文件全路径并读取配置参数获取XML文件全路径的方法有两个,一是在servlet中获取,二是在单独的JAVA类中获取。1.在servlet中获取XML文件的全路径并读取配置参数程序片段String dirPath = getServletContext().getRealPath( "/WEB-INF"); String fileName = dirPath + "/conn.xml"; ReadXml xm = new ReadXml(fileName); String DriverName = xm.getDrivername(); String connStr = xm.getSConnStr(); String user = xm.getUsername(); String pas = xm.getPassword();将这段程序添加到servlet中dopost()之后即可完成参数的读取2.在单独的JAVA类中获取全路径并读取配置参数程序片段String dirpath = System.getProperty("user.dir"); String xmlFile = dirpath + "/WebRoot/WEB-INF/conn.xml"; ReadXml rdxml = new ReadXml(xmlFile); String driverName = rdxml.getDrivername(); String sConnStr = rdxml.getSConnStr(); String userName = rdxml.getUsername(); String passWord = rdxml.getPassword();注:配置文件conn.xml保存在webroot/WEB-INF目录中。

⑷ 如何通过读取XML配置文件导入Excel文件到数据中

1.首先,我们正常使用Excel的另存为,看看能否顺利地直接另存为XML文件。

方法:

点击Excel左上角按钮,在弹出的选项中,点击“另存为”

或者直接在Excel中按下快捷键F12

⑸ asp.net怎么对xml读取数据和写入数据

//读取XmlDocumentdoc=newXmlDocument();doc.Load(Server.MapPath("person.xml"));//加载Xml文件XmlElementroot=doc.DocumentElement;//获取根节点XmlNodeListpersonNodes=root.GetElementsByTagName("Person");//获取Person子节点集合foreach(XmlNodenodeinpersonNodes){ stringid=((XmlElement)node).GetAttribute("id");//获取Name属性值 stringname=((XmlElement)node).GetElementsByTagName("Name")[0].InnerText;//获取Age子XmlElement集合 stringage=((XmlElement)node).GetElementsByTagName("Age")[0].InnerText; Console.WriteLine("编号:"+id+"姓名:"+name+"年龄:"+age);}//添加XmlDocumentdoc=newXmlDocument();doc.Load(Server.MapPath("person.xml"));XmlElementroot=doc.DocumentElement;//根节点的添加独立子节点XmlElementperson=doc.CreateElement("Person");person.SetAttribute("id","3");person.AppendChild(getChildNode(doc,"Name","Elephant"));person.AppendChild(getChildNode(doc,"Age","23"));root.AppendChild(person);doc.Save(Server.MapPath("person.xml"));Console.WriteLine("XML文件节点添加成功");//编辑XmlDocumentdoc=newXmlDocument();doc.Load(Server.MapPath("person.xml"));//加载Xml文件XmlElementroot=doc.DocumentElement;//获取根节点XmlNodeListpersonNodes=root.GetElementsByTagName("Person");//获取Person子节点集合foreach(XmlNodenodeinpersonNodes){XmlElementele=(XmlElement)node;if(ele.GetAttribute("id")=="2"){ XmlElementnameEle=(XmlElement)ele.GetElementsByTagName("Name")[0]; nameEle.InnerText=nameEle.InnerText+"修改";}}Console.WriteLine("节点修改成功");doc.Save(Server.MapPath("person.xml"));//删除XmlDocumentdoc=newXmlDocument();doc.Load(Server.MapPath("person.xml"));//加载Xml文件XmlElementroot=doc.DocumentElement;//获取根节点XmlNodeListpersonNodes=root.GetElementsByTagName("Person");//获取Person子节点集合XmlNodeselectNode=root.SelectSingleNode("/Persons/Person[@id='1']");root.RemoveChild(selectNode);Console.WriteLine("节点删除成功");doc.Save(Server.MapPath("person.xml"));

⑹ C# XMl读写配置文件

<config><serv_ip>192.168.0.1</serv_ip><connect_time>30</connect_time><refresh_time>60</refresh_time><serv_port>3000</serv_port></config>

一般像这样就行了 很简单的

或者多个配置你可以加个item标签如:

<config><item><serv_ip>192.168.0.1</serv_ip><connect_time>30</connect_time><refresh_time>60</refresh_time><serv_port>3000</serv_port></item><item><serv_ip>192.168.0.2</serv_ip><connect_time>30</connect_time><refresh_time>60</refresh_time><serv_port>3000</serv_port></item></config>

假定文件路径为:path

读取和保存:

usingsystem.xml;XmlDocumentxml=newXmlDocument();//声明xmlxml.Load(path);//按路径读xml文件xmlnoderoot=xml.selectsinglenode("config");//指向根节点xmlnodexn=root.selectsinglenode("serv_ip");//指向根节点下的serv_ip节点stringip=xn.innertext;//读出里面的值注意读取的是string需要类型转换的话自己做//修改:xn.innertext="192.168.2.2";xml.save(path);//保存更改到路径

⑺ Java读取xml配置文件:

import java.io.File;import java.net.MalformedURLException;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;/*test.xml内容<company-config><company id="1" name="hideCompanyMobile">13823095539,111,222</company></company-config>*/public class Demo_01 { public static void main(String[] args) { File inputXml = null; SAXReader saxReader = null; Document document = null; List list = null; try { inputXml = new File("D:/test.xml"); saxReader = new SAXReader(); document = saxReader.read(inputXml); list = document.selectNodes("//company-config/company[@id='1']"); if(list.size()>0){ Element element = (Element)list.get(0); String[] strArray = element.getText().split(","); System.out.println(strArray[0]); System.out.println(strArray[1]); System.out.println(strArray[2]); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } finally { list = null; document = null; saxReader = null; inputXml = null; } }}

⑻ bat 读取xml配置文件

bat文件如何读取ini配置文件作为x的参数_网络知

⑼ 如何编写或从XML配置文件中读取

配置文件是XML配置文件(配置) config文件中会有像许多条目<name>abcd</name><company>xyz</company><choise>choise1</choise>

NSIS共有4 XML的插件可供选择; NsisXML(由Wizou).xML插件,NsisXML(乔尔)和NSXML 使用NsisXML(由Wizou):Outfile "$%temp%NSISTest.exe"RequestExecutionLevel userInstalldir "$Temp"Showinstdetails show!include LogicLib.nshPage InstFiles!define XMLFILE "$instdirmyxml.xml"SectionStrCpy $9 "Did not exist"nsisXML::creatensisXML::load "${XMLFILE}"${If} $0 = 0;build a new basic XML file:nsisXML::creatensisXML::createProcessingInstruction "xml" 'version="1.0" encoding="UTF-8" standalone="yes"'nsisXML::appendChildnsisXML::release $2${EndIf}nsisXML::select '/choise'${If} $2 = 0StrCpy $1 $0nsisXML::createElement "choise"nsisXML::appendChild${Else}nsisXML::getTextStrCpy $9 $3${EndIf}DetailPrint "Old value: $9"System::Call 'kernel32::GetTickCount()i.r5' ;Get some "random" value to savensisXML::setText "$5"nsisXML::release $2nsisXML::save "${XMLFILE}"nsisXML::release $0DetailPrint "Saved new value: $5"SectionEnd

在优先次运行我得到:Old value: Did not existSaved new value: 709289703Completed并在第二次运行我得到:Old value: 709289703Saved new value: 709308687Completed


赞 (0)