Skip to main content
Inspiring
August 3, 2011
Question

Extract And SET Values from XML File

  • August 3, 2011
  • 2 replies
  • 1183 views

I have XML string like this

<Request><Header><UserId>pgh</UserId><Token>05032015T1105144</Token></Header></Request>

How can I extract the <UserId> and <Token> values only?

So for the above string --I want the values

pgh

05032015T1105144

I want to set these 2 values to variables like this...everything else in the string goes away

<CFSET userid =pgh>

<CFSET token = 05032015T1105144>

So you START with this

<Request><Header><UserId>pgh</UserId><Token>05032015T1105144</Token></Header></Request>

And END with this

<CFSET userid =pgh>

<CFSET token = 05032015T1105144>

    This topic has been closed for replies.

    2 replies

    pete_freitag
    Participating Frequently
    August 3, 2011

    There are a few ways you could do this, but the easiest is probably to use the XmlParse function, eg:

    <cfset xmlObj = XmlParse(xml)>

    You should then be able to do something like this:

    <cfoutput>

    #xmlObj.XmlRoot.Header.UserId#

    #xmlObj.XmlRoot.Header.Token#

    </cfoutput>

    You can do a cfdump on the xmlObj to see all the variables at your disposal. You might need to append an XML declaration to the beginning of your XML, eg:

    <?xml version="1.0" encoding="UTF-8"?>

    Inspiring
    August 3, 2011

    Looks like this is related to your other thread.  How far did you get with things, were you able to parse the XML?

    weezerboyAuthor
    Inspiring
    August 3, 2011

    Yes it is related to the post you mention.

    I was able to extract the info....

    now the partner firm is requesting that I send back this xml (see below) to them...any idea on how I would do that?

    </Response

    Inspiring
    August 3, 2011

    The forum must have eaten your code.  Try using the insert code button  ie  ">>".

    now the partner firm is requesting that I send back this xml (see below) to them...any idea on how I would do that?

    But we seem to have switched gears. Are you saying you have figured out how to extract values from your XML and now need to generate XML instead?