JAVA读取XML文件详细解析
JAVA读取XML文件详细解析
1.有很多时候我们需要将相关动态的信息保存到XML文件中去,那么我们就可以动态输入相关信息。特别是针对数据库方面的操作的时候就非常地灵活。那我现在假如项目中的操作数据库的包为model里面存放了我们的XML文件db.xml现在来看看如何解析出来相关的数据来调用。
第一个类:
package model; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.SAXException; import java.util.Properties; //使用DefaultHandler的好处 是 不必陈列出所有方法, public class ConfigParser extends DefaultHandler { ////定义一个Properties 用来存放 dbhost dbuser dbpassword的值 private Properties props; private String currentSet; private String currentName; private StringBuffer currentValue = new StringBuffer(); //构建器初始化props public ConfigParser() { this.props = new Properties(); } public Properties getProps() { return this.props; } //定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来. public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException { currentValue.delete(0, currentValue.length()); this.currentName =qName; } //这里是将<xxx></xxx>之间的值加入到currentValue public void characters(char[] ch, int start, int length) throws SAXException { currentValue.append(ch, start, length); } //在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中 public void endElement(String uri, String localName, String qName) throws SAXException { props.put(qName.toLowerCase(), currentValue.toString().trim()); } } 可以直接拷贝过来用了。 |
第二个类:
package model; import java.util.Properties; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import java.net.URL; public class ParseXML{ //定义一个Properties 用来存放 dbhost dbuser dbpassword的值 private Properties props; //这里的props public Properties getProps() { return this.props; } public void parse(String filename) { //将我们的解析器对象化 ConfigParser handler = new ConfigParser(); //获取SAX工厂对象 SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); //获取SAX解析 SAXParser parser=null; try { parser = factory.newSAXParser(); } catch (Exception e1) { e1.printStackTrace(); } URL confURL = null; //得到配置文件myenv.xml所在目录. tomcat中是在WEB-INF/classes //下例中BeansConstants是用来存放xml文件中配置信息的类,可以自己代替或定义 try{ confURL = ParseXML.class.getClassLoader().getResource(filename); //只需要将我们所需要的XML文件名字输入进去就可以了! }catch(Exception e){ System.out.print(e.toString()); } try { //将解析器和解析对象myenv.xml联系起来,开始解析 parser.parse(confURL.toString(), handler); //获取解析成功后的属性 以后 我们其他应用程序只要调用本程序的props就可以提取出属性名称和值了 props = handler.getProps(); }catch(Exception e){ System.out.println(e.toString()); }finally{ factory=null; parser=null; handler=null; } } } |
下面是我们所需要调用的XML文件内容:
<myenv> <datasource> <dbhost>localhost</dbhost> <dbname>tianya_speed_test</dbname> <dbuser>root</dbuser> <dbpassword>321</dbpassword> </datasource> </myenv> |
现在需要将其中的相关内容读取出来。包括数据库主机地址,数据库名字,用户名及密码好了,现在准备工作已经都做完了。现在就可以来想想如何来调用我们刚才解析出来的内容喽:
public void initVariable(){ ParseXML test = new ParseXML(); try{ test.parse("model/db.xml");//表示实际的XML文件位置,如果没有找到就报空指针异常! }catch(Exception e){ System.out.print(e.toString()); } Properties pro = test.getProps();//获取到PRO对象然后用它去调用相关的属性! dbServer = pro.getProperty("dbhost");//相当于Python中的字典类型。 dbName = pro.getProperty("dbname"); dbUser = pro.getProperty("dbuser"); dbPwd = pro.getProperty("dbpassword"); } |
tcp.validnode_checking=yes #允许访问的ip tcp.invited_nodes=(ip1,ip2,……) #不允许访问的ip tcp.excluded_nodes=(ip1,ip2,……) |
好了,现在我们就只需要手工来修改XML就可以了。
以后在做项目过程中如果需要用XML来配置数据库或其他的相关信息就可以了用这个方法哦。比较简单而且实用!
评论暂时关闭