Skip to main content
BreakawayPaul
Inspiring
May 28, 2014
Answered

How do I deal with an XML file that uses html characters?

  • May 28, 2014
  • 2 replies
  • 2900 views

I need to pull a userlist for my web app, and that involves invoking a web service on the authentication server.  I've managed to do just that, but the XML file I'm given to deal with looks like this:

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

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<soapenv:Body><ns1:getAllUserInfoResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://web.ws">

<getAllUserInfoReturn xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">

&lt;?xml version = '1.0'?&gt;

&lt;USER&gt;

&lt;ROW num=&quot;1&quot;&gt;

&lt;USER_ID&gt;25214&lt;/USER_ID&gt;

&lt;LOGIN_ID&gt;JDOE&lt;/LOGIN_ID&gt;

&lt;NAME_LAST&gt;JOHN&lt;/NAME_LAST&gt;

&lt;NAME_FIRST&gt;DOE&lt;/NAME_FIRST&gt;

&lt;NAME_MI&gt;Q&lt;/NAME_MI&gt;

&lt;NAME_FI&gt;A&lt;/NAME_FI&gt;

&lt;EMAIL&gt;john.doe@email.com&lt;/EMAIL&gt;

&lt;OFFICE_PHONE&gt;5551212&lt;/OFFICE_PHONE&gt;

&lt;OFFICE_STREET1&gt;123 Main Street&lt;/OFFICE_STREET1&gt;

&lt;/ROW&gt; &lt;/USER&gt;

( etc )

</getAllUserInfoReturn>

</ns1:getAllUserInfoResponse>

</soapenv:Body>

</soapenv:Envelope>

I need to loop through this and get usernames, ids, rights, etc, but with it looking like that, I have my work cut out for me (I think).  I'm not sure if there's an easy way to replace entities in an XML file (the docs seem to be down), and if there is, I don't know about it.

Any ideas?

    This topic has been closed for replies.
    Correct answer BKBK

    BreakawayPaul wrote:

    I need to loop through this and get usernames, ids, rights, etc, but with it looking like that, I have my work cut out for me (I think).  I'm not sure if there's an easy way to replace entities in an XML file

    Assuming you have already obtained the XML file using something like <cffile action="read" variable="xmlFile">, then the following single line of code is sufficient:

    <cfset xmlDoc=xmlParse(replaceList(xmlFile,'&lt;,&gt;,&quot;','<,>,"'),false)>

    Note, however, that your XML would then contain an error. The processing instruction, "<?xml version = '1.0'?>", occurs twice.

    2 replies

    BKBK
    BKBKCorrect answer
    Community Expert
    May 29, 2014

    BreakawayPaul wrote:

    I need to loop through this and get usernames, ids, rights, etc, but with it looking like that, I have my work cut out for me (I think).  I'm not sure if there's an easy way to replace entities in an XML file

    Assuming you have already obtained the XML file using something like <cffile action="read" variable="xmlFile">, then the following single line of code is sufficient:

    <cfset xmlDoc=xmlParse(replaceList(xmlFile,'&lt;,&gt;,&quot;','<,>,"'),false)>

    Note, however, that your XML would then contain an error. The processing instruction, "<?xml version = '1.0'?>", occurs twice.

    BreakawayPaul
    Inspiring
    May 29, 2014

    Eddie Lotter wrote:

    The document servers are available now.

    Have a look at xmlParse: Adobe ColdFusion 9 * XmlParse

    Is this the same for CF8 (which we're currently stuck on)?

    BKBK wrote:

    Assuming you have already obtained the XML file using something like <cffile action="read" variable="xmlFile">, then the following single line of code is sufficient:

    <cfset xmlDoc=xmlParse(replaceList(xmlFile,'&lt;,&gt;,&quot;','<,>,"'),false)>

    Note, however, that your XML would then contain an error. The processing instruction, "<?xml version = '1.0'?>", occurs twice.

    I'm getting the XML file from a web service, using cfhttp.  As a result, my data is contained in userlist.filecontent.

    I can do this to get rid of the escaped characters (thanks):

    <cfset myusers= replaceList(userlist.filecontent,'&lt;,&gt;,&quot;','<,>,"')>

    But this errors out:

    <cfset listusers= xmlParse(myusers)>

    (error parsing xml document)

    BreakawayPaul
    Inspiring
    May 29, 2014

    Ew, it looks like the problem is with the XML format itself.  I did xmlvalidate() and got this:

    struct
    errors
    array
    1 [Error] :1:214: cvc-elt.1: Cannot find the declaration of element 'soapenv:Envelope'.
    2 [Error] :1:454: cvc-elt.4.2: Cannot resolve 'soapenc:string' to a type definition for element 'getAllUserInfoReturn'.
    fatalerrors
    array
    1 [Fatal Error] :1:459: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    status NO
    warnings
    array [empty]

    I might have to go about this another way.

    EddieLotter
    Inspiring
    May 28, 2014

    The document servers are available now.

    Have a look at xmlParse: Adobe ColdFusion 9 * XmlParse