Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

reading an xml document in coldfusion

New Here ,
Jul 08, 2011 Jul 08, 2011

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

2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 08, 2011 Jul 08, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 08, 2011 Jul 08, 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#">

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 09, 2011 Jul 09, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 13, 2011 Jul 13, 2011

Also, you should also be able to use the local-name() XPath method to perform a namespace-agnostic search of your document:

e.g. the XPath statement

//*[local-name() = 'MyNodeName']

Should return any instances of the <MyNodeName> element in your document regardless of what the XML namespace on that element is.  Otherwise, Adam's suggestion should work fine.  Alternatively, you could always just Nuke the namespaces with some fance RegEx footwork, but that opens you up to possible XML element name collisions and will break any sort of XML DTD/Schema validation.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 12, 2011 Jul 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2011 Jul 16, 2011
LATEST

<!--- 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources