Skip to main content
N_Bridges
Participant
August 28, 2014
Question

XML Parser

  • August 28, 2014
  • 1 reply
  • 196 views

Hi,

i use CF to parse an XML FILE, i'm stucking now with this part:

<HR.PUB.CONTENT>

<FT TYPE="F">Example GmbH</FT>

, now in Zurich,

<FT TYPE="A">CHE-100.000.000</FT>

, City new:

<FT TYPE="S">Erlenbach ZH</FT>

. Address new: Haldenstrasse, 8700 Erlenbach ZH.

</HR.PUB.CONTENT>


I just want one string like that:

Example GmbH, now in Zurich, CHE-100.000.000, city news: Erlenbach ZH. Address new: Haldenstrasse, 8700 Erlenbach ZH.


i tried this one, but it wasn't useful:

Text= xmlsearch(MyData, "//HR01/HR01.SPEC/HR.PUB.CONTENT")[idxAttr1].XmlText



Can anyone help me? I use <cfscript>



This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
August 29, 2014

XML is quite suited to the text of the FT elements. But not for the inline text, now in Zurich, City new:, etc.


You could do it in 2 parts. String parsing (as list) for the inline text, and XML parsing (as associative array) for the XmlText.

<cfset str = "">

<!--- string --->

<cfsavecontent variable="strDoc"><HR.PUB.CONTENT>

<FT TYPE="F">Example GmbH</FT>

, now in Zurich,

<FT TYPE="A">CHE-100.000.000</FT>

, City new:

<FT TYPE="S">Erlenbach ZH</FT>

. Address new: Haldenstrasse, 8700 Erlenbach ZH.

</HR.PUB.CONTENT></cfsavecontent>

<!--- xml --->

<cfxml variable="xmlDoc"><cfoutput>#strDoc#</cfoutput></cfxml>

<!--- Pick out the Xmltext --->

<cfset elemText = arrayNew(1)>

<cfset n = 1>

<cfloop array="#xmldoc['HR.PUB.CONTENT'].xmlChildren#" index="FT_elem">

<cfset elemText = FT_elem.XmlText>

<cfif n LT arrayLen(xmldoc['HR.PUB.CONTENT'].xmlChildren)>

<cfset n++>

</cfif>

</cfloop>

<!--- In the string, replace the FT elements and their contents with the delimiter "|", creating a suitable list --->

<cfset inlineText = REreplace(strDoc,"<FT\b[^>]*>(.*?)</FT>","|","all")>

<!--- Take an entry from the array and the next immediate entry from the list --->

<cfloop from="1" to="#n#" index="idx">

<cfset str = str & elemText[idx] & listGetAt(inlineText,idx+1,'|') >

</cfloop>

<cfoutput>#str#</cfoutput>