Java讀取XML實現反射例項
阿新 • • 發佈:2019-01-25
* XML(Extensible Markup Language,可擴充套件標記語言)
1、XML DOM解析:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); File f = ... Document doc=builder.parse(f); URL u=... Document doc=builder.parse(u); InputStream in=... Document doc=builder.parse(in);
2、XML DTD(Document Type Definition,文件型別驗證)驗證:
- XML中驗證:<!DOCTYPE configuration [<!Element configuration ...> more rules ...]>
- 外部dtd檔案驗證:<!DOCTYPE configuration SYSTEM "config.dtd">
- URL驗證:<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
3、XML Schema:其本身就是一個XML檔案,同時其內容指明瞭某種XML的格式。
關於dtd和XML Schema文件的編寫可以參考《Java核心技術 卷II》Page 152-154中給出的具體檔案內容示例。
例項主要邏輯:
- HelloWorld.java提供反射機制訪問的類
- test.xml檔案給出HelloWorld類的資訊
- Test.java檔案讀取xml中的類資訊,並利用反射機制生成HelloWorld類的例項,並呼叫其中的方法
具體檔案如圖所示
各檔案內容如下
HelloWorld.java
import java.util.Arrays; public class HelloWorld { public void sayHello(){ System.out.println("sya hello"); } public void sayHello(int i){ System.out.println("sya hello: " + i); } public void sayHello(String[] str){ System.out.println("say hello: " + Arrays.toString(str)); } public static void main(String[] args) { String[] str = {"a", "b"}; HelloWorld h = new HelloWorld(); h.sayHello(str); System.out.println("hello world"); } }
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<element>
<import class="HelloWorld"/>
</element>
Test.java
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
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 Test {
/**
* @param args
* @throws IOException
* @throws ParserConfigurationException
* @throws SAXException
* @throws ClassNotFoundException
* @input
*/
public static void main(String[] args) throws Exception, ParserConfigurationException, SAXException, ClassNotFoundException {
DocumentBuilder builder;
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
if (args.length == 0)
return;
else {
InputStream in=null;
try {
//讀取XML檔案
String filePath = args[0];
File file = new File(filePath);
in = new FileInputStream(file);
Document doc=builder.parse(in);
Element eRoot = doc.getDocumentElement();
NodeList nl = eRoot.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
if (!(n instanceof Element))
continue;
Element e = (Element) n;
System.out.println("Element: " + e.getNodeName());
//取得類名
String className=e.getAttribute("class");
Class cl=Class.forName(className);
//建立類例項物件
Object o=cl.newInstance();
//動態呼叫方法
Method m=cl.getMethod("sayHello", String[].class);
Object[] ob=new Object[1];
String arg[]=new String[1];
arg[0]="1";
ob[0]=arg;
m.invoke(o,ob);
}
}
finally {
in.close();
}
}
}
}
具體執行方法:
- javac HelloWorld.java
- javac -encoding utf-8 Test.java
- java Test test.xml
輸出:
最後檔案: