Under Construction...
Very often its required to have a child node from a huge xml required to be extracted without having the xmlparse applied.
What's wrong with doing "XMLPARSE" and then accessing the desired child node.?
1. if xml is huge it's going to take time and resources.
2. if you are suppose to send xml as string to DB, then you can do xmlparse().serialize because you might loose original special or html chars with serialization.
3. If you want to "Remove" particular child/sub node from an existing, xmlparse() and then structDelete will be expense.
The better options is using the "RegEx",
1. If you want to extract child node from an existing xml node (without using xmlparse)
Use regex to find the child node and extract its entire content out as a string.
2. If you want to remove a particular xml child/node and its entire content from an existing xml
use regex to find the "child" block of xml you desire and then do reReplace to take it out.
The below method does simple step which is,
"On the given xml string, it extracts the specified sub expression from the first instance of a regular expression match"
Usage Example:
<cfset local.myxml = '<Data
xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance">
<ParentUser sourceID="1">
<Name>Manufacturer Test</Name>
<WebSiteURL>www.not_a_real_URL.us</WebSiteURL>
<Contact>
<ContactName>Some name</ContactName>
<EmailAddress>some@email.org</EmailAddress>
<PhoneNumber>1-111-222-111</PhoneNumber>
</BillingContact>
<User sourceID="2">
<FirstName>username</FirstName>
<LastName>mylastname</LastName>
<EmailAddress>myemail@some.org</EmailAddress>
<PhoneNumber>1-999-222-1111</PhoneNumber>
</User>
<Active>true</Active>
</ParentUser >
<NewUser sourceID="406431">
<FirstName>NewUser</FirstName>
<LastName>NewUser</LastName>
<EmailAddress>NewUser@NewUser.org</EmailAddress>
<PhoneNumber>1-999-111-1111</PhoneNumber>
<Active>true</Active>
</NewUser>
</Data>' />
<cfset local.regex = "(<User.*?</User>)" />
<cfset local.res = reExtract (local.myxml, local.regex, "1") />
<cfset local.res = '<Data xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance">#local.res#</Data>' />
<cfset local.res = xmlparse (local.res) /> <!--- for debugging purpose --->
<cfdump var =#local# abort />
<cffunction name="reExtract" output="false" hint="Extracts the specified sub expression from the first instance of a regular expression match."> <cfargument name="string" required="true" hint="The string to search."> <cfargument name="regex" required="true" hint="Regex to search with. (Must contain parenthesis for subexpressions.)"> <cfargument name="pos" required="true" hint="Which subexpression match to return 'ALL' for an array of all subexpression matches."> <cfargument name="caseSensitive" default="false" hint="Should the match be concerned with case?"> <!--- removed-local-scope ---> <cfscript> if(arguments.caseSensitive) { local.find = reFind(arguments.regex, arguments.string, 1, true); } else { local.find = reFindNoCase(arguments.regex, arguments.string, 1, true); } if(arguments.pos eq "ALL") { local.ret = []; for(local.c = 1; local.c <= arrayLen(local.find.pos); local.c++) { if(local.find.pos[local.c] > 0 AND local.find.len[local.c] > 0) { local.ret[local.c] = mid(arguments.string, local.find.pos[local.c], local.find.len[local.c]); } } } else { local.ret = ""; if(arrayLen(local.find.pos) gte arguments.pos AND local.find.pos[arguments.pos] > 0 AND local.find.len[1] > 0){ local.ret = mid(arguments.string, local.find.pos[arguments.pos], local.find.len[arguments.pos]); } } </cfscript> <cfreturn local.ret/> </cffunction>