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

Retrieve XML

Participant ,
Oct 31, 2010 Oct 31, 2010

Copy link to clipboard

Copied

I have XML that looks like this

<?xml version="1.0" encoding="utf-8"?>
<rsp>
 
<videos total="39" >
  <video id="565455" title="With Love" />
 
</videos>
</rsp>

I'm trying to dispay the xml using coldfusion. I've done this before but the problem i have this time is trying to access the id and title values in the video.

Can someone give me a simple example of retrieving this?

I have:

<cfset mydoc = XmlParse(CFHTTP.FileContent)>
<cfset pb = mydoc.rsp.XmlChildren>
<cfset size = ArrayLen(pb)>

<cfset myquery = QueryNew("id, title")>
<cfset temp = QueryAddRow(myquery, #size#)>

<cfloop index="i" from = "1" to = #size#>

??

</cfloop>

Sorry if my cf is so bad!!

TOPICS
Advanced techniques

Views

1.4K

Translate

Translate

Report

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 ,
Oct 31, 2010 Oct 31, 2010

Copy link to clipboard

Copied

Have a look @ XPath & xmlSearch().

A good beginners' tutorial on XPath:

http://www.zvon.org/comp/r/tut-XPath_1.html

xmlSearch() docs:

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-78e8.html

Have you also read the XML section of the CF docs?

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fb3.html

It pretty-much covers everything (... OK... most things...) you need to know about accessing/addressing XML data with CF.


Probably in this case - if your XML really is that straight fwd, I'd just access the values directly with dot-notation, as per the CF docs above, in which it's all explained.

--

Adam

Votes

Translate

Translate

Report

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
Participant ,
Oct 31, 2010 Oct 31, 2010

Copy link to clipboard

Copied

Hi Adam, Thanks so much.

I couldnt find how to access a value inside the tag.

Could you send me to a direct example. I think the info is little over my head.

Votes

Translate

Translate

Report

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 ,
Oct 31, 2010 Oct 31, 2010

Copy link to clipboard

Copied

Did the stuff here - http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec133ba-7fe6.html#WSc3ff6d0ea77859461172e0811cbec22c24-78e9 - not make sense?

What did you try?  Where's some code that you tried?

I'm not one to just give people "direct" answers here, sorry: I don't believe that this helps much.

However I'm happy to help you work through it, if you give it a go yourself.

--

Adam

Votes

Translate

Translate

Report

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
Participant ,
Oct 31, 2010 Oct 31, 2010

Copy link to clipboard

Copied

My code is up there. I just don't know how to retrieve the value of the attribute.

In this example, how can I figure it out without seeing the XML that coldfusion is parsing

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec133ba-7fe6.html#WSc3ff6d0ea77859461172e0811cbec22c24-78e9

Votes

Translate

Translate

Report

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
Participant ,
Oct 31, 2010 Oct 31, 2010

Copy link to clipboard

Copied

Sorry the xml document is on the page. Let me take another look

Votes

Translate

Translate

Report

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
Participant ,
Oct 31, 2010 Oct 31, 2010

Copy link to clipboard

Copied

This is what I have now

<rsp>
 
<videos total="39" >
  <video id="565455" title="With Love" />
 
</videos>
</rsp>

<cfset mydoc = XmlParse(CFHTTP.FileContent)>
<cfset pb = mydoc.rsp.XmlChildren>
<cfset size = ArrayLen(pb)>

<cfset myquery = QueryNew("id, title")>
<cfset temp = QueryAddRow(myquery, #size#)>

<cfloop index="i" from = "1" to = #size#>


<cfset temp = QuerySetCell(myquery, "id", #mydoc.videos.video[1].XmlAttributes["id"]#, #i#)>  Error here
<cfset temp = QuerySetCell(myquery, "title", #mydoc.videos.video[1].XmlAttributes["title"]#, #i#)>


</cfloop>

I'm getting this error:

Element VIDEOS.VIDEO is undefined in MYDOC.

I think the problem is the XML doc I'm parsing has an extra parent <rsp>

Votes

Translate

Translate

Report

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
Enthusiast ,
Nov 01, 2010 Nov 01, 2010

Copy link to clipboard

Copied

You have two problems in the code you've posted.

1. The size variable you've defined will contain the number of children for the "rsp" node which is one, "videos".  You want to get the number of "video" elements.

2. When using element names you can put them in square brackets with quotes.

See code below.


<cfxml variable="mydoc">
    <?xml version="1.0" encoding="utf-8"?>
    <rsp>
      <videos total="39" >
        <video id="565455" title="With Love" />
        <video id="12345" title="Item Two" />
      </videos>
    </rsp>
</cfxml>


<cfset size=ArrayLen(mydoc["rsp"]["videos"]["video"])> <!--- get number of video elements --->

<cfloop index="i" from = "1" to = #size#>

    <cfoutput>id: #mydoc["rsp"]["videos"]["video"].XmlAttributes["id"]#, title: #mydoc["rsp"]["videos"]["video"].XmlAttributes["title"]#</cfoutput>

</cfloop>

Votes

Translate

Translate

Report

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
Participant ,
Nov 01, 2010 Nov 01, 2010

Copy link to clipboard

Copied

Thanks. At this line

<cfset size=ArrayLen(mydoc["rsp"]["videos"]["video"])> <!--- get number of video elements --->

I'm getting:

Element videos is undefined in a Java object of type class coldfusion.xml.XmlNodeList.

My code:

<cfset mydoc = XmlParse(CFHTTP.FileContent) />
<cfset size=ArrayLen(mydoc["rsp"]["videos"]["video"])>

<cfloop index="i" from = "1" to = #size#>

    <cfoutput>id: #mydoc["rsp"]["videos"]["video"].XmlAttributes["id"]#</cfoutput >

</cfloop>

Votes

Translate

Translate

Report

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
Enthusiast ,
Nov 02, 2010 Nov 02, 2010

Copy link to clipboard

Copied

I am not able to re-create the error using the code below.  I suspect that the XML contained in the CFHTTP.FileContent variable does not match the sample you provided.

<!--- use CFSAVECONTENT instead of CFHTTP --->

<cfsavecontent variable="fileContent"><?xml version="1.0" encoding="utf-8"?>
    <rsp>
      <videos total="39" >
        <video id="565455" title="With Love" />
        <video id="12345" title="Item Two" />
      </videos>
    </rsp>
</cfsavecontent>

<cfset mydoc = XmlParse(fileContent)> 
<cfset size=ArrayLen(mydoc["rsp"]["videos"]["video"])>

<cfloop index="i" from = "1" to = #size#>

    <cfoutput>id: #mydoc["rsp"]["videos"]["video"].XmlAttributes["id"]#</cfoutput>

</cfloop>

Votes

Translate

Translate

Report

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
Participant ,
Nov 02, 2010 Nov 02, 2010

Copy link to clipboard

Copied

I'm wondering if maybe the API key is failing and that's why it's not bringing it up. I'll check this out.

Votes

Translate

Translate

Report

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
Participant ,
Nov 04, 2010 Nov 04, 2010

Copy link to clipboard

Copied

Say in the case of

    <cfoutput>id: #mydoc["rsp"]["videos"]["video"].XmlAttributes["id"]#</cfoutput>

How do I reference a tag name with colons

<media:group></media:group>


Votes

Translate

Translate

Report

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 ,
Nov 04, 2010 Nov 04, 2010

Copy link to clipboard

Copied

LATEST

Those colons are called namespaces.

http://www.aftergeek.com/2006/08/xmlsearch-xpath-and-xml-namespaces-in.html

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/

http://training.figleaf.com/

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on

GSA Schedule, and provides the highest caliber vendor-authorized

instruction at our training centers, online, or onsite.

Read this before you post:

http://forums.adobe.com/thread/607238

Votes

Translate

Translate

Report

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
Documentation