Skip to main content
Participating Frequently
July 8, 2011
Question

reading an xml document in coldfusion

  • July 8, 2011
  • 3 replies
  • 2056 views

I am trying to read the following xml document. could some one let me know how can i get the text - DDOTDirector

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

   <DataSet xmlns="http://tempuri.org/">

      <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

         <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">

            <xs:complexType>

                <xs:choice minOccurs="0" maxOccurs="unbounded">

                   <xs:element name="Table">

                       <xs:complexType>

                          <xs:sequence>

                             <xs:element name="DDOTDirector" type="xs:string" minOccurs="0" />

                          </xs:sequence>

                        </xs:complexType>

</xs:element>

</xs:choice>

</xs:complexType>

</xs:element>

</xs:schema>

</DataSet>

Thank You

srinivasa

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    Community Expert
    July 16, 2011

    <!--- Covert to XML document object --->
    <cfxml variable="xmlDoc"><?xml version="1.0" encoding="utf-8"?>
          <DataSet xmlns="http://tempuri.org/">
          <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
             <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                <xs:complexType>
                    <xs:choice minOccurs="0" maxOccurs="unbounded">
                       <xs:element name="Table">
                           <xs:complexType>
                              <xs:sequence>
                                 <xs:element name="DDOTDirector" type="xs:string" minOccurs="0" />
                              </xs:sequence>
                            </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>
    </DataSet></cfxml>

    <!--- Match the element whose name is 'element' and which has a type attribute whose value is 'xs:string'--->
    <cfset elementArray=xmlSearch(xmlDoc, "//*[local-name() = 'element' and @type='xs:string']")>

    <!--- <cfdump var="#elementArray#"> --->

    <!--- Match the value of the attribute called 'name' --->
    <cfoutput>#elementArray[1].xmlAttributes["name"]#</cfoutput>

    Participating Frequently
    July 12, 2011

    I have had problems in the past with ColdFusion and processing XML data, I was required to create a webservice that communicated data in an XML format and it was frustrating at first but then when I had it working I was very happy.

    Usually the first thing you do with XML data in ColdFusion is to use the function XMLPARSE.

    However, this function will error when there are leading and following empty spaces or new lines before and after the XML data. To get rid of these you can use the TRIM function with the XMLPARSE function and that often cleans up the errors fairly well.

    In this case, you can spend lots of aggravating time trying to determine what the problem is with your XML data, but using the TRIM function like the following can help.

    For example:

    <cfset XMLDATA = Some XML Data>

    <cfset XMLOBJECT = XmlParse(Trim(XMLDATA))>

    Hope this helps with your issues, let the forum know if you are able to solve your issue.

    Michael G. Workman

    michael.g.workman@gmail.com

    Inspiring
    July 8, 2011

    Well come on... what have you tried so far?  We're here to help you with yor code, not write it for you!

    --

    Adam

    Participating Frequently
    July 8, 2011

    I tried many. i cant post all what i have tried but i can post this.

    <cfset

    myxmlfile=ExpandPath("preapproved_scripts.xml")>

    <cffile action="read" file="#myxmlfile#" variable="myxmlcode">

    <cfset myxml=xmlparse(myxmlcode)>

    <cfdump var="#MYXML#">

    <cfset BookNodes = xmlSearch(myxml,'/Dataset/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element')>

    <cfdump var="#BookNodes#">

    Inspiring
    July 9, 2011

    OK, I had to do three things to get this to work.

    * xpath is case-sensitive, so you need to reference "DataSet" not "Dataset" in your xmlSearch() statement;

    * CF didn't seem to know how to deal with the namespaces for the xmlSearch() unless they were in the <DataSet> node, not the <xs:schema> node.  Now: as far as I can tell, it's completely fine to have it the way you've done it as ar as the XML side of things is concerned: and even CF parses it fine, as you know, but xmlSearch() didn't like it.  I'm pretty ignorant when it comes to XML namespaces, but judging by the behaviour of all the other bits, it looks like a bug in xmlSearch() to me.  Or the way CF builds its XML objects from the XML source doc, or something.  I could - of course - be wrong.

    * given you're using namespaces, you need to use them *everywhere* in your xpath string.

    So:

    I changed this:

    <DataSet xmlns="http://tempuri.org/">

    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

    To this:

    <DataSet xmlns="http://tempuri.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

    <xs:schema id="NewDataSet">

    And this:
    /Dataset/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element
    To this:
    /:DataSet/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element
    Then it worked.
    --
    Adam