通过Groovy在Java中进行XML解析

我正在尝试使用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>"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;
  }
}

输出

{1=["Catcher In the Rye",J.D. Salinger],2=["KiteRunner",Khaled Hosseini]}

解决方法

你的Groovy脚本可能是“groovy-er”……

这也是一样的:

String fact = "def getBookinformation(n) {" +
                "  xmlDesc = new XmlSlurper().parse(n)\n" +
                "  xmlDesc.book.findAll().collectEntries {\n"+
                "    [ (it.@id):[ it.name,it.author ] ]\n" +
                "  }\n" +
                "}" ;

实际上,您可以使用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 ;
  }
}

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...