Bir XML'i Groovy ve Java ScriptEngine API'sini kullanarak ayrıştırmaya çalışıyorum. Aşağıdaki kod tam olarak bunu yapar ama aynı şeyi yapmanın daha iyi bir yolu olup olmadığını öğrenmek istedim. Ve ayrıca bununla ilgili herhangi bir performans etkisi varsa?Groovy yoluyla Java'da XML ayrıştırma
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/*
<books>
<book id="1">
<name>"Catcher In the Rye"</name>
<author>J.D. Salinger</author>
</book>
<book id="2">
<name>"KiteRunner"</name>
<author>Khaled Hosseini</author>
</book>
</books>
*/
public class XMLParsing{
public static void main(String[] args) {
Map<String, ArrayList<String>> resultMap
= new HashMap<String, ArrayList<String>>();
resultMap = getBookDetails("c:\\temp\\book.xml");
System.out.println(resultMap);
}
public static Map<String ArrayList<String>> getBookDetails(String scriptXml) {
Map<String, ArrayList<String>> resultMap =
new HashMap<String, ArrayList<String>>();
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
String fact = "import java.util.HashMap;"
+ "import java.util.ArrayList;"
+ "def getBookInformation(n){"
+ "def map1 = new HashMap();"
+ "xmlDesc = new XmlSlurper().parse(n);"
+ "xmlDesc.book.findAll{it}.each {"
+ "def list1 = new ArrayList();"
+ "def id = [email protected];"
+
//"println id;"+
"def name = it.name;"
+ "def author = it.author;"
+ "list1.add(name);"
+ "list1.add(author);"
+ "map1.put(id, list1);"
+ "};"
+ "return map1;}";
try {
engine.eval(fact);
Invocable inv = (Invocable) engine;
Object[] params = {scriptXml};
resultMap = (Map<String,ArrayList<String>>)
inv.invokeFunction("getBookInformation", params);
} catch (ScriptException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return resultMap;
}
}
Çıktı:
{1=["Catcher In the Rye", J.D. Salinger], 2=["KiteRunner", Khaled Hosseini]}
thankx çok Rob! – Arham