1

I am using groovy 1.8 where i want to have a list something like this
List<MyQueryClass> queryl=new List<MyQueryClass>()
So that using the below loop i can add more to this list, how can I do that?

def queryxml=new XmlSlurper().parse(QueryProvider.class.getResourceAsStream( '/queries.xml' ))
    queryxml.query.each { node ->
        operation="${[email protected]()}"
        if(operation.equals(op.create.toString()))
        {
            query="${node.text()}"
            println "$query"
            MyQueryClass myQuery=new MyQueryClass (query)
            queryl.add(myQuery)
        }           
    }
2
  • where did you define queryl? this seems that it should be working if you define queryl above queryxml... what error do you get? Commented May 26, 2011 at 10:30
  • I define queryl above this code, when i write List<MyQueryClass> queryl=new List<MyQueryClass>() i get Groovy:You cannot create an instance from the abstract interface 'java.util.List' error Commented May 27, 2011 at 4:46

2 Answers 2

3

My apologies, I could not convey my question properly.
I figured it out, as I cannot create an instance out of a abstract interface java.util.List

List<MyQueryClass> queryl=new ArrayList<MyQueryClass>() 

does the work. Thanks tim and draganstankovic

Sign up to request clarification or add additional context in comments.

Comments

1

You don't give us much to go on, but assuming you have a class:

@groovy.transform.Canonical class MyQuery {
  String queryText
}

And your XML is similar to:

def xml = '''<xml>
  <query operation="create">
    This is q1
  </query>
  <query operation="q2">
    This is q2
  </query>
  <query operation="create">
    This is q3
  </query>
</xml>'''

Then you should be able to use findAll to limit the xml to just the nodes of interest, followed by collect to build up your list of objects like so:

def queryxml = new XmlSlurper().parseText( xml )

List<MyQuery> query = queryxml.query.findAll { node ->  // Filter the nodes
  [email protected]() == 'create'
}.collect { node ->                                     // For each matching node, create a class
  new MyQuery( node.text().trim() )
}

println query

And that prints out:

[MyQuery(This is q1), MyQuery(This is q3)]

PS: The groovy.transform.Canonical annotation is explained here

4 Comments

I think somewhere I was not able to question properly, B4 I was doing something like List<MyQueryClass> queryl=new List<MyQueryClass>[5] and once i added I got 6 as its size, but actually only 1 query was getting added, so my question was how to do it dynamically, I figured it out, as I cannot create an instance out of a abstract interface java.util.List So finally i did List<MyQueryClass> queryl=new ArrayList<MyQueryClass>() to make it work :) Thanks though for answering, I'll give you an up for giving an answer
@Abhishek As you're using groovy, you shouldn't need to do new ArrayList<MyQueryClass>() at all...
what should I be using instead, because if i use List<MyQueryClass> queryl=new List<MyQueryClass>() i get an error mark where it says Groovy:You cannot create an instance from the abstract interface 'java.util.List'
@Abhishek Have you looked at the code I posted? That generates a list from the collect call. Or, you could just do: List<MyQueryClass> queryl = [] which does the same as your line of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.