通过Groovy在Java中进行XML解析 [英] XML parsing in Java via Groovy
问题描述
我试图使用Groovy和Java的ScriptEngine API来解析XML。
下面的代码完全是这样,但我想知道是否有更好的方法来做同样的事情。还有,如果有任何与此相关的性能影响?
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>麦田守望者< / name>
< author> J.D。塞林格< /作者>
< / 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\\.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;};
尝试{
engine.eval(fact);
Invocable inv =(Invocable)引擎;
Object [] params = {scriptXml};
resultMap =(Map< String,ArrayList< String>>)
inv.invokeFunction(getBookInformation,params);
} catch(ScriptException e){
e.printStackTrace();
} catch(NoSuchMethodException e){
e.printStackTrace();
}
返回resultMap;
$ / code $ / pre
$ b $输出:
{1 = [麦田守望者,JD Salinger],2 = [KiteRunner,Khaled Hosseini]}
$ c $您的Groovy脚本可能是groovy-er ...。
这也是一样的:
String fact =def getBookInformation(n){+
xmlDesc = new XmlSlurper()。parse(n)\\\
+
xmlDesc.book.findAll()。collectEntries {\ n+
[(it。@ id):[it.name,it.author]] \\\
+
} \ n+
};
确实,您可以使用 GroovyShell
而不是JVM脚本引擎,并且可以让你得到:
$ b $ pre $ import $ java.util.ArrayList;
import java.util.Map;
导入groovy.lang.Binding;
导入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;
}
}
I'm trying to parse an XML using Groovy and the ScriptEngine API of Java.
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"...
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:
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 ;
}
}
这篇关于通过Groovy在Java中进行XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!