Question

I am trying to parse:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><LoginResponse xmlns="http://services.marketernet.com/application"><LoginResult><results><response value="UY+/9dD+Lz7DT3Oq/WG3CVJ/pFW7o6LEFNA4xOSIWr88Dh2RVAgy9qHP1BwpdiYA"/><exceptions></exceptions></results></LoginResult></LoginResponse></soap:Body></soap:Envelope>

So far I have:

<cfset soapResponse = xmlParse(httpResponse.fileContent) />
<cfset results = xmlSearch(soapResponse,"//*[local-name()='LoginResult' and namespace-uri()='http://services.marketernet.com/application']") />

I need the value of <response value="UY+/9dD+Lz7DT3Oq/WG3CVJ/pFW7o6LEFNA4xOSIWr88Dh2RVAgy9qHP1BwpdiYA"/>

I try looping, even try to do a deep xml path, nothing.

Please help me, if you have questions please let me know.

Update 1: "ScreenShot" ScreenShot

Update 2: "Screenshot long version" Screenshot

Was it helpful?

Solution

I normally just use xmlSearch(soapResponse,"//*[local-name()='whatever']") and it works fine for me. It can return different types depending on how deep you search in the XML. Because of that, when developing the code I always use <cfdump> to view the results of the xmlSearch() function to know what I am dealing with.

I took the SOAP response that you shared and tested the following code successfully on ColdFusion 9.0.1. Notice that I have three different searches here each delving deeper into the XML tree. I left the <cfdump> in there so you can see what each returns.

<cftry>
<cfsavecontent variable="content">
    <?xml version="1.0" encoding="UTF-8" ?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Body>
            <LoginResponse xmlns="http://services.marketernet.com/application">
                <LoginResult>
                    <results>
                        <response value="UY+/9dD+Lz7DT3Oq/WG3CVJ/pFW7o6LEFNA4xOSIWr88Dh2RVAgy9qHP1BwpdiYA"/>
                        <exceptions></exceptions>
                    </results>
                </LoginResult>
            </LoginResponse>
        </soap:Body>
    </soap:Envelope>
</cfsavecontent>
<cfset soapResponse = xmlParse(Trim(content)) />

<html>
    <head><title>Test xmlParse</title></head>
    <body>
        <h3>xmlParse option 1</h3>
        <div>
            <cfset results = xmlSearch(soapResponse,"//*[local-name()='LoginResult']") />
            <cfdump var="#results#" />
            <cfset value = results[1].results.response.XmlAttributes.value />
            <cfdump var="#value#" />
        </div>
        <h3>xmlParse option 2</h3>
        <div>
            <cfset results = xmlSearch(soapResponse,"//*[local-name()='results']") />
            <cfdump var="#results#" />
            <cfset value = results[1].response.XmlAttributes.value />
            <cfdump var="#value#" />
        </div>
        <h3>xmlParse option 3</h3>
        <div>
            <cfset results = xmlSearch(soapResponse,"//*[local-name()='response']") />
            <cfdump var="#results#" />
            <cfset value = results[1].XmlAttributes.value />
            <cfdump var="#value#" />
        </div>
    </body>
</html>
<cfcatch type="any">
    <cfdump var="#cfcatch#" />
</cfcatch>
</cftry>

All of the options result in setting the value variable to UY+/9dD+Lz7DT3Oq/WG3CVJ/pFW7o6LEFNA4xOSIWr88Dh2RVAgy9qHP1BwpdiYA from the XML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top