title: XmlSlurper to parse XML and get value of inside elements using Groovy
body: <p>I am trying to parse the below XML:</p>
  
  <pre><code>    &lt;body&gt;
        &lt;section id="5f884f20-6638-461f-a3f5-3d237341c048" outputclass="definition_and_scope"&gt;
          &lt;title&gt;Definition and Scope&lt;/title&gt;
          &lt;p&gt;A work that is modified for a purpose, use, or medium other than that for which it was originally intended.&lt;/p&gt;
          &lt;p&gt;This relationship applies to changes in form or to works completely rewritten in the same form.&lt;/p&gt;
        &lt;/section&gt;
        &lt;section id="a7cf019f-dc82-46e2-b5ae-2e947d3c2509" outputclass="popup:ready_reference"&gt;
          &lt;title&gt;Element Reference&lt;/title&gt;
          &lt;div id="8472e205-3a32-40e3-a7ea-8bd7dbd43715" outputclass="iri"&gt;
            &lt;p id="e6ddf17a-6b4b-4de3-886e-a315d88545ea" outputclass="title"&gt;
              &lt;b&gt;IRI&lt;/b&gt;
            &lt;/p&gt;
            &lt;p id="c69f6279-27a3-4cd8-84a6-bb2c5a7b0424"&gt;
              &lt;xref format="html" href="http://rdaregistry.info/Elements/w/P10142" scope="external"&gt;http://rdaregistry.info/Elements/w/P10142&lt;/xref&gt;
            &lt;/p&gt;
          &lt;/div&gt;
          &lt;div id="3e979983-cbac-4982-84c7-57ae9756e2bb" outputclass="domain"&gt;
            &lt;p id="9815dbdf-7483-4dcf-8166-7ea50138b3e5" outputclass="title"&gt;
              &lt;b&gt;Domain&lt;/b&gt;
            &lt;/p&gt;
            &lt;p id="328a1035-1eaf-4c4b-aead-d604586b3f64"&gt;
              &lt;xref keyref="rdacC10001/ala-c3e1fff8-0a79-35c6-bee1-39b6b4c9ed35"&gt;Work&lt;/xref&gt;
            &lt;/p&gt;
          &lt;/div&gt;
          &lt;div id="13163eda-dcfd-48d9-aea4-cc8abef2f675" outputclass="range"&gt;
            &lt;p id="d07d4e37-dff1-4561-baab-f8f557d99662" outputclass="title"&gt;
              &lt;b&gt;Range&lt;/b&gt;
            &lt;/p&gt;
            &lt;p id="3873a6ab-5f73-47e2-9daa-441169e66c36"&gt;
              &lt;xref keyref="rdacC10001/ala-c3e1fff8-0a79-35c6-bee1-39b6b4c9ed35"&gt;Work&lt;/xref&gt;
            &lt;/p&gt;
          &lt;/div&gt;
      &lt;/section&gt;
     &lt;/body&gt;
  </code></pre>
  
  <p>I want to extract the values of all the p tags inside of section &amp; section/div and append that value to a stringbuilder.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>def docText = new StringBuilder();
  def bodyObject = doc.topic.body.toXmlString(true) //I have only pasted a part of my XML in this question. My XML starts with a doc/topic/body etc
  def parseBodyObject = new XmlSlurper().parse(new InputSource(new StringReader(bodyObject)));
  def findAllSection = parseBodyObject.depthFirst().findAll{it.name()=='section'}
  
  findAllSection.each {section-&gt;
  
        docText.append(" " +section.p)
        docText.append(" " +section.div.p + " ")
  
  }
  </code></pre>
  
  <p>Output:
  My docText looks like below:</p>
  
  <pre><code>A work that is modified for a purpose, use, or medium other than that for which it was originally intended.This relationship applies to changes in form or to works completely rewritten in the same form. IRIhttp://rdaregistry.info/Elements/w/P10142DomainWorkRangeWorkAlternate labelsUser tasksRecording methodsDublin Core TermsMARC 21 Bibliographic Recording an unstructured descriptionRecording a structured descriptionRecording an identifierRecording an IRI For the inverse of this element, see Work: adapted as work For broader elements, see Work: based on workFor narrower elements, see
  </code></pre>
  
  <p>I am stuck at adding a space between text. For eg. When it is going through section/div/p, it is adding all the p together without any spaces as below:</p>
  
  <pre><code>IRIhttp://rdaregistry.info/Elements/w/P10142DomainWorkRangeWorkAlternate
  </code></pre>
  
  <p>which should output as(expected output):</p>
  
  <pre><code>IRI http://rdaregistry.info/Elements/w/P10142 Domain Work 
  </code></pre>
  
  <p>How should I get these values separated? Any help is appeciated. </p>