📄 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 + -