关于html5:java-如何读取xml

45次阅读

共计 5644 个字符,预计需要花费 15 分钟才能阅读完成。

1.DB.java

package com.bn.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DB {
private static String dbServer;
private static String dbName;
private static String dbUser;
private static String dbPwd;
//java 页游我的项目 fhadmin.org
public void readXML(){

 
 SAXReader sr = new SAXReader();// 获取读取 xml 的对象。Document doc = null;
 String path1 = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));
 //System.out.println(path1);
 path1 = path1 + "../DB.xml";
 //System.out.println(path1);

try {

doc = sr.read(path1);

} catch (DocumentException e) {

// TODO Auto-generated catch block
e.printStackTrace();

}// 失去 xml 所在位置。而后开始读取。并将数据放入 doc 中

Element el_root = doc.getRootElement();// 向外取数据,获取 xml 的根节点。Iterator it = el_root.elementIterator();// 从根节点下顺次遍历,获取根节点下所有子节点

 while(it.hasNext()){// 遍历该子节点
 
  Object o = it.next();// 再获取该子节点下的子节点
 Element el_row = (Element)o;
  String s = el_row.getText();
  Iterator it_row = el_row.elementIterator();
  int i =1;
  while(it_row.hasNext()){// 遍历节点
  Element el_ename = (Element)it_row.next();// 获取该节点下的所有页游数据。if(i == 1){this.setDbServer(el_ename.getText());
    //System.out.println(this.getDbServer());
    //dbServer = el_ename.getText();}
   if(i == 2){this.setDbName(el_ename.getText());
     // System.out.println(this.getDbName());
    //dbName = el_ename.getText();}
   if(i == 3){this.setDbUser(el_ename.getText());
     // System.out.println(this.getDbUser());
    //dbUser = el_ename.getText();}
   if(i == 4){this.setDbPwd(el_ename.getText());
     // System.out.println(this.getDbPwd());
    //dbPwd = el_ename.getText();}
  // System.out.println(i);
   i++;
   //System.out.println(el_ename.getText());
  }
  //System.out.println(o);
 }
 
}

static {
DB dbxml = new DB();
dbxml.readXML();

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}
}
public static Connection createConn(){
DB dbxml = new DB();
//String name = dbxml.dbName;
//System.out.www.pizei.comprintln(name);
//System.out.println(dbxml.getDbServer() + dbxml.getDbName() + dbxml.getDbUser() + dbxml.getDbPwd());
Connection conn = null;
try {

conn = DriverManager.getConnection("jdbc:mysql://"+ dbxml.getDbServer() +":3306/"+ dbxml.getDbName() +"?user="+ dbxml.getDbUser() +"&password="+ dbxml.getDbPwd());
//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bnss?user=root&password=root");

} catch (SQLException e) {

e.printStackTrace();

}
return conn;
}
public static PreparedStatement createPstmt(Connection conn, String sql){
PreparedStatement pstmt = null;
try {

pstmt = conn.prepareStatement(sql);

} catch (SQLException e) {

e.printStackTrace();

}
return pstmt;
}
public static void close(Connection conn){
if(conn == null)return;
try {

conn.close();
conn = null;

} catch (SQLException e) {

e.printStackTrace();

}
}
public static void close(Statement stmt){
try {

stmt.close();
stmt = null;

} catch (SQLException e) {

e.printStackTrace();

}
}
public static void close(ResultSet rs){
try {

rs.close();
rs = null;

} catch (SQLException e) {

e.printStackTrace();

}
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public String getDbServer() {
return dbServer;
}
public String getDbUser() {
return dbUser;
}
public String getDbPwd() {
return dbPwd;
}
public void setDbServer(String dbServer) {
this.dbServer = dbServer;
}
public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}
public void setDbPwd(String dbPwd) {
this.dbPwd = dbPwd;
}
}
2.ParseXML.java

package com.bn.util;
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;

//java 我的项目 fhadmin.org
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;
     }
 }

}
3.ConfigParser.java

package com.bn.util;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import java.util.Properties;
// 应用 DefaultHandler 的益处 是 不用排列出所有办法,
//java 我的项目 fhadmin.org
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());
 }

}

正文完
 0