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 3223456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:06:33+00:00 2026-05-17T16:06:33+00:00

I have following xml file: <doc_xml> <nodes> <node id=’1′ spec={spec_a=0.9, spec_b=0.1} /> <node id=’2′

  • 0

I have following xml file:

<doc_xml>
<nodes>
  <node id='1' spec="{spec_a=0.9, spec_b=0.1}" />
  <node id='2' spec="{spec_a=0.1, spec_b=0.3}" />
  <node id='3' spec="{}" />
</nodes>
</doc_xml>

This code was created using Groovy MarkupBuilder.

Now I would like to parse this file in a groovy script:

def xml = new XmlParser().parseText(getDocXmlAsString());

xml.nodes.node.each {
   Map spec = it.@spec; // here I got an exception org.codehaus.groovy.runtime.typehandling.GroovyCastException

}

but I keep getting this exception:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{spec_a=0.9, spec_b=0.1}' with class 'java.lang.String' to class 'java.util.Map'

My question, how to parse an xml attribute which is a map?

  • 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-05-17T16:06:34+00:00Added an answer on May 17, 2026 at 4:06 pm

    I suspect the code and xml you posted are not the actual code and xml you are having troubles with…

    However, assuming you meant to close the <node/> tags when posting your example xml, I tried this Groovy code:

    def xmlStr = """<doc_xml>
    <nodes>
      <node id='1' spec="{spec_a=0.9, spec_b=0.1}"/>
      <node id='2' spec="{spec_a=0.1, spec_b=0.3}"/>
      <node id='3' spec="{}"/>
    </nodes>
    </doc_xml>"""
    
    def xml = new XmlParser().parseText( xmlStr )
    def spec = [:]
    
    xml.nodes.node.each {
      spec = it.@spec
      println spec
    }
    

    And it works, and prints out:

    {spec_a=0.9, spec_b=0.1}
    {spec_a=0.1, spec_b=0.3}
    {}
    

    These are Strings, not Maps as you seem to want…

    To get them as Maps, you could do:

    xml.nodes.node.each {
      spec = Eval.me( it.@spec.tr( '{=}', '[:]' ) )
      println spec
    }
    

    You need the tr call, to convert the format you have chosen for your maps into a format groovy can handle…

    As you say you generate the XML, can I suggest you change your xml to:

    <doc_xml>
    <nodes>
      <node id='1' spec="[spec_a:0.9, spec_b:0.1]"/>
      <node id='2' spec="[spec_a:0.1, spec_b:0.3]"/>
      <node id='3' spec="[:]"/>
    </nodes>
    </doc_xml>
    

    As then, you can skip the tr() step…or use Json or something other than your bespoke format?

    Not sure storing a map in an attribute is a good way forward…it feels a little bit brittle to me :-/

    EDIT

    I see what you mean, it is the Groovy MarkupBuilder adding that strange formating when it adds an attribute as a Map…

    Maybe one solution would be to do something like this?

    import groovy.xml.MarkupBuilder
    
    // Imaginary data that we're going to generate our XML from:
    def nodeData = [
      [ id:1, spec_a:0.9, spec_b:0.1 ],
      [ id:2, spec_a:0.1, spec_b:0.3 ],
      [ id:3 ]
    ]
    
    def writer = new StringWriter()
    def xml = new MarkupBuilder(writer)
    xml.doc_xml() {
      nodes() {
        nodeData.each {
          node( it )
        }
      }
    }
    
    def xmlStr = writer.toString()
    
    println "Write out the XML"
    println xmlStr
    
    def xmlParse = new XmlParser().parseText( xmlStr )
    def spec = [:]
    
    println "Write out the Attributes for each node"
    xmlParse.nodes.node.each {
      spec = it.attributes()
      println spec
    }
    

    That would output:

    Write out the XML
    <doc_xml>
      <nodes>
        <node id='1' spec_a='0.9' spec_b='0.1' />
        <node id='2' spec_a='0.1' spec_b='0.3' />
        <node id='3' />
      </nodes>
    </doc_xml>
    Write out the Attributes for each node
    [id:1, spec_a:0.9, spec_b:0.1]
    [id:2, spec_a:0.1, spec_b:0.3]
    [id:3]
    

    As you can see, each map entry is added as an attribute, and these can be extracted back out using the attributes() call on each Node class from the XmlParser

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
I have an XML file with the following line: <VALUE DECIMAL_VALUE=0.2725 UNIT_TYPE=percent/> I would
Say i have the following xml file: <a> <b> <c></c> </b> <b> <c></c> </b>
I have the following xml file: <?xml version=1.0 encoding=UTF-8 standalone=no ?> <config> <a> <b>
I have an XML file like this: <SiteConfig> <Sites> <Site Identifier=a /> <Site Identifier=b
Lets say I have the following XML file: <?xml version=1.0 encoding=utf-8?> <Customers xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation=Customer.xsd>
I am using the following code to retrieve data from the xml file. It
Assume I have the following XML file: <Movies> <Movie ownerName=Ryan> <Title>The Lord of the
I have the following logback.xml file: <configuration debug=true> <appender name=STDOUT class=ch.qos.logback.core.ConsoleAppender> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread]
I have following XML configration i would like to convert to java property file.

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.