?? xmlparserutil.java
字號:
package myjdbc.util;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
import java.io.IOException;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
public class XMLParserUtil {
private Document document;
public XMLParserUtil(String configFile) throws ParserConfigurationException,SAXException,IOException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//告訴工廠將所需解析器類型
dbf.setValidating(false);
//使用工廠獲取JAXP 解析器對象
DocumentBuilder parser = dbf.newDocumentBuilder();
// 告訴解析器如何處理錯誤,注意在JAXP API 中
// DOM 解析器依賴SAX API處理錯誤
parser.setErrorHandler(new ErrorHandler(){
public void warning(SAXParseException e) {
System.err.println("WARNING: "+e.getMessage());
}
public void error(SAXParseException e) {
System.err.println("ERROR: " + e.getMessage());
}
public void fatalError(SAXParseException e) throws SAXException {
System.err.println("FATAL: " + e.getMessage());
throw e; // 重新拋出錯誤
}
}
);
// 最后使用JAXP 解析器解析文件。此調用返回
// 一個Document 對象,有了這個對象,類的
// 其余部分使用DOM API處理它,不再需要JAXP
document = parser.parse(configFile);
}
public Object[] getFieldAndColumnMapping(String _tableName) throws Exception{
String[] columns = null;
String[] classFileds = null;
Element root = (Element)document.getElementsByTagName("table-config").item(0);
Element subroot = (Element)root.getElementsByTagName("class").item(0);
//subroot元素的name屬性的屬性值
String className = subroot.getAttribute("name");
//subroot元素的table屬性的屬性值
String tableName = subroot.getAttribute("table");
if (! _tableName.equalsIgnoreCase(tableName))
throw new Exception("請檢查配置文件中的table屬性值!");
//得到所有property元素,并取得property元素的column屬性值
NodeList nodelist = subroot.getElementsByTagName("property");
int len = nodelist.getLength();
columns = new String[len];
classFileds = new String[len];
for (int i = 0; i <len; i++) {
Element temp = (Element)nodelist.item(i);
columns[i] = temp.getAttribute("column");
classFileds[i] = temp.getAttribute("name");
}
return new Object[]{className,columns,classFileds};
}
public static void main(String args[]) throws Exception{
String file = "classes/conf/wb_relationdetail.xml";
XMLParserUtil parser = new XMLParserUtil(file);
Object[] temp = parser.getFieldAndColumnMapping("wb_relationdetail");
///////////////////////
System.out.println("class name = "+ temp[0]);
////////////////////////////////////
Object[] columns = (Object[])temp[1];
for(int i=0,len = columns.length; i<len ; i++)
{
System.out.println("column name = " + columns[i]);
}
///////////////
Object[] fields = (Object[])temp[2];
for(int i=0,len = fields.length; i<len ; i++)
{
System.out.println("field name = " + fields[i]);
}
//testReflect((String)temp[0]);
}
// private static void testReflect(String className)throws Exception
// {
//
// Object ob = Class.forName(className).newInstance();
// Class obClass = ob.getClass();
// Method[] methods = obClass.getMethods();
// for (int i = 0, len = methods.length; i < len; i++) {
// Method method = methods[i];
//
// }
// }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -