Skip to main content
nikos101
Inspiring
June 16, 2010
Question

XMLParse problem when using XML

  • June 16, 2010
  • 1 reply
  • 353 views

CF seems to have problems parsing XML that starts like this:

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

<ArrayOfRateInfo>

instead of

<ArrayOfRateInfo xmlns="http://schemas.datacontract.org/2004/07/SingletonRateService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

Can you advise why this is so?

XmlSearch never returns data when using the latter example even when usign XMLParse

<cfset xmlCCY=XMLParse("http://site.com/rateservice.svc/")>

<cfscript>
      selectedElements = XmlSearch(xmlCCY, "/ArrayOfRateInfo");
     for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
         writeoutput(selectedElements.XmlText & "<br>");
</cfscript>

    This topic has been closed for replies.

    1 reply

    Inspiring
    June 16, 2010

    Nope, CF doesn't have problems with that sort of thing, as quickly demonstrated here:

    <cfset sX = '<?xml version="1.0" encoding="UTF-8"?><ArrayOfRateInfo><foo>bar</foo></ArrayOfRateInfo>'>

    <cfset x = xmlParse(sX)>
    <cfdump var="#x#">

    <cfset a = xmlSearch(x, "/ArrayOfRateInfo/foo")>
    <cfdump var="#a#"> 

    So whatever issue you're having, it ain't that.

    CF is flaky when there's anything before the <?xml?> tag though.  Like whitespace or a BOM.

    What error are you getting?

    Also, in your second example, you need to indicate in your xpath expression that you're not using a prefix for your namespace, like this:

    <cfset a = xmlSearch(x, "/:ArrayOfRateInfo/:foo")>

    Why?  Dunno... I have no idea about XML namespaces... I just googled that.

    --

    Adam