[英]XML parsing in Java via Groovy
I'm trying to parse an XML using Groovy and the ScriptEngine API of Java. 我正在尝试使用Groovy和Java的ScriptEngine API来解析XML。 The code below does exactly that but I wanted to know if there are any better ways of doing the same.
下面的代码正是如此,但我想知道是否有更好的方法来做同样的事情。 And also if there are any performance implications related to this?
还有与此相关的性能影响吗?
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 = it.@id;"
+
//"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;
}
}
Output: 输出:
{1=["Catcher In the Rye", J.D. Salinger], 2=["KiteRunner", Khaled Hosseini]}
Your Groovy script could be "groovy-er" ... 你的Groovy脚本可能是“groovy-er” ......
This does the same thing: 这也是一样的:
String fact = "def getBookInformation(n) {" +
" xmlDesc = new XmlSlurper().parse(n)\n" +
" xmlDesc.book.findAll().collectEntries {\n"+
" [ (it.@id):[ it.name, it.author ] ]\n" +
" }\n" +
"}" ;
Indeed, you could use the GroovyShell
rather than the JVM scripting engine, and that gets you down to: 实际上,您可以使用
GroovyShell
而不是JVM脚本引擎,这可以帮助您:
import java.util.ArrayList;
import java.util.Map;
import groovy.lang.Binding ;
import groovy.lang.GroovyShell ;
public class XMLParsing {
public static void main(String[] args) {
Map<String, ArrayList<String>> resultMap = getBookDetails("test.xml");
System.out.println(resultMap);
}
public static Map<String, ArrayList<String>> getBookDetails( String scriptXml ) {
Binding b = new Binding() ;
b.setVariable( "xmlFile", scriptXml ) ;
GroovyShell shell = new GroovyShell( b ) ;
Object ret = shell.evaluate( "new XmlSlurper().parse( xmlFile ).book.findAll().collectEntries { [ (it.@id):[ it.name, it.author ] ] }" ) ;
return (Map<String, ArrayList<String>>)ret ;
}
}
In order to make ScritpEngine more performant, we could use Compilable interface. 为了使ScritpEngine更高效,我们可以使用Compilable接口。 The code below is a mix of novelty from Tim's comments and the discussion here .
下面的代码是Tim的评论和这里的讨论的新颖性。
public static Map<String, ArrayList<String>> getBookDetails(String scriptXml) {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");
engine.put("xmlFile", scriptXml);
try {
if (engine instanceof Compilable) {
CompiledScript script = ((Compilable) engine).compile( "new XmlSlurper().parse( xmlFile ).book.findAll().collectEntries { [ (it.@id):[ it.name, it.author ] ] }" );
return (Map<String, ArrayList<String>>)(script.eval());
}
} catch (ScriptException e) {
e.printStackTrace();
}
return null;
}
Output: 输出:
{1=["Catcher In the Rye", J.D. Salinger], 2=["KiteRunner", Khaled Hosseini]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.