Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8635243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:55:41+00:00 2026-06-12T09:55:41+00:00

I’m trying to parse an XML using Groovy and the ScriptEngine API of Java.

  • 0

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]}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:55:42+00:00Added an answer on June 12, 2026 at 9:55 am

    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 ;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.