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

  • Home
  • SEARCH
  • 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 7798053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:53:08+00:00 2026-06-01T23:53:08+00:00

I have a little xml-parsing problem with Groovy. This is, what my data looks

  • 0

I have a little xml-parsing problem with Groovy.
This is, what my data looks like:

ID      001=1234
sID     001=q8b6v
d1      001=some Text
d2      001=more Text2
tzh     001=data
dse     001=data
ID      001=567823
sID     001=l3n37v2
d1      001=some Text
d2      001=more Text2
hrg     001=data
dfe     001=data
...

I need ID, sID and d1.

What I get:

What I get:
<add>
  <doc>
    <field name='ID'>00573419</field>
    <field name='sID'>20120110572</field>
    <field name='d1'>some Text</field>
    <field name='ID'>00573406</field>
    <field name='sID'>20120111110</field>
    <field name='d1'>some Text2</field>
  </doc>
</add>

What I want:

<add>
  <doc>
    <field name='ID'>00573419</field>
    <field name='sID'>20120110572</field>
    <field name='d1'>some Text</field>
  </doc>
  <doc>
     <field name='ID'>00573406</field>
     <field name='sID'>20120111110</field>
     <field name='d1'>some Text2</field>
  </doc>
</add>

That’s part of my code:

if(!dataFile.exists()){
        println "File does not exist!"
    }else{

        def key1, key2, key3, key4
        def val1, val2, val3, val4

        xml.add{
            doc {
                dataFile.eachLine {line ->
                    if(line.startsWith(regExId)){
                        (key1, val1) = line.split(/001=/).collect {it.trim()}
                        field(name:key1, val1)
                    }
                    if(line.startsWith(regExEntryId)){
                        (key2, val2) = line.split(/001=/).collect {it.trim()}
                        field(name:key2, val2)
                    }
                    if(line.startsWith(regExTitle)){
                        (key3, val3) = line.split(/001=/).collect {it.trim()}
                        field(name:key3, val3)
                    }
                }
            }
        }

I want to keep it as simple as possible. So what I need is a statement like ‘if here starts a new regExId then start a new xml-block’, so that I can easily add new parts of the data (like d2) as needed. There is no existing xml-file.
Thanks for any information!

  • 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-01T23:53:10+00:00Added an answer on June 1, 2026 at 11:53 pm

    This is what I came up with:

    import groovy.xml.MarkupBuilder
    
    dataFile = new File( 'data.txt' )
    
    // A closure that will add a new doc element to our xml
    def addDoc = { builder, data ->
      builder.doc {
        data.each { k, v ->
          builder.field( name:k, v )
        }
      }
    }
    
    
    String result = new StringWriter().with { out -> // create a StringWriter
      new MarkupBuilder( out ).with { xml ->         // pass it to MarkupBuilder
        add {                                        // create our root level element
          def data = [:]
          // Then, for each line in our input file
          dataFile.eachLine { line ->
            // Get the key and value as you had it
            def (key,value) = line.split( /001=/ )*.trim()
            // Just use the keys we want
            if( key in [ 'ID', 'sID', 'd1' ] ) {
              // If we have a new ID and we have data
              if( key == 'ID' && data ) {
                // Add it to the document
                addDoc( xml, data )
                // And start again with a new map
                data = [ ID: value ]
              }
              else {
                // Otherwise, just add the key to the map
                data[ key ] = value
              }
            }
          }
          // So, we've run out of lines. If we have data, write it in
          if( data ) addDoc( xml, data )
        }
      } 
      // Convert our StringWriter to a String (this goes into the variable `result)
      out.toString()
    }
    // And print it out
    println result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little bit of XML that looks like this: <LayerDefinition xsi:type =GridLayerDefinition>
I have a little problem about Google Maps. I'm using Geocoding (xml) for getting
I have little unusual problem to solve. I need some hint or links to
I have a little script that replace some text, that come from a xml
I am currently trying to parse some xml data into an NSDictionary . This
I have this little problem I couldn't find in Google: UITableView works fine until
I have some XML that I am parsing in python via lxml. I am
I have little question about how web browser retrieve webpage? I know this User
I have little doubt about string reading in C. string reading functions like gets,
i have little dilemma, i often use data-bound controls such as Gridview in conjunction

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.