Я пытаюсь проанализировать приведенный ниже XML:
<body>
<section id="5f884f20-6638-461f-a3f5-3d237341c048" outputclass="definition_and_scope">
<title>Definition and Scope</title>
<p>A work that is modified for a purpose, use, or medium other than that for which it was originally intended.</p>
<p>This relationship applies to changes in form or to works completely rewritten in the same form.</p>
</section>
<section id="a7cf019f-dc82-46e2-b5ae-2e947d3c2509" outputclass="popup:ready_reference">
<title>Element Reference</title>
<div id="8472e205-3a32-40e3-a7ea-8bd7dbd43715" outputclass="iri">
<p id="e6ddf17a-6b4b-4de3-886e-a315d88545ea" outputclass="title">
<b>IRI</b>
</p>
<p id="c69f6279-27a3-4cd8-84a6-bb2c5a7b0424">
<xref format="html" href="http://rdaregistry.info/Elements/w/P10142" scope="external">http://rdaregistry.info/Elements/w/P10142</xref>
</p>
</div>
<div id="3e979983-cbac-4982-84c7-57ae9756e2bb" outputclass="domain">
<p id="9815dbdf-7483-4dcf-8166-7ea50138b3e5" outputclass="title">
<b>Domain</b>
</p>
<p id="328a1035-1eaf-4c4b-aead-d604586b3f64">
<xref keyref="rdacC10001/ala-c3e1fff8-0a79-35c6-bee1-39b6b4c9ed35">Work</xref>
</p>
</div>
<div id="13163eda-dcfd-48d9-aea4-cc8abef2f675" outputclass="range">
<p id="d07d4e37-dff1-4561-baab-f8f557d99662" outputclass="title">
<b>Range</b>
</p>
<p id="3873a6ab-5f73-47e2-9daa-441169e66c36">
<xref keyref="rdacC10001/ala-c3e1fff8-0a79-35c6-bee1-39b6b4c9ed35">Work</xref>
</p>
</div>
</section>
</body>
Я хочу извлечь значения всех тегов p внутри секции & section / div и добавить это значение в stringbuilder.
Вот мой код:
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->
docText.append(" " +section.p)
docText.append(" " +section.div.p + " ")
}
Вывод: Мой docText выглядит следующим образом:
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
Я застрял при добавлении пробела между текстом.Например,Когда он проходит через раздел / div / p, он складывает все p вместе без пробелов, как показано ниже:
IRIhttp://rdaregistry.info/Elements/w/P10142DomainWorkRangeWorkAlternate
, который должен выводиться как (ожидаемый результат):
IRI http://rdaregistry.info/Elements/w/P10142 Domain Work
Как я должен разделить эти значения?Любая помощь определена.