Solved

Targetting attributes and values in XML

Posted on 2006-06-27
1
787 Views
Last Modified: 2013-11-19
Ok i'm having trouble getting values from an XML document, heres the XML returned:
<?xml version="1.0"?>
<methodResponse> <params><param><value><string>
<photo id="123456508" secret="12345687e2" server="12" dateuploaded="1150921931" isfavorite="0" license="0" rotation="0" originalformat="jpg">
     <owner nsid="12336959@N00" username="BoozeSanDiego" realname="BoozeSanDiego" location="USA" />
     <title>Group</title>
     <description>Everyone in mex</description>
     <visibility ispublic="1" isfriend="0" isfamily="0" />
     <dates posted="1150921931" taken="2006-06-21 13:32:11" takengranularity="0" lastupdate="1151449682" />
     <editability cancomment="0" canaddmeta="0" />
     <comments>2</comments>
     <notes />
     <tags>
          <tag id="1239387-172173508-239" author="12336959@N00" raw="party">party</tag>
          <tag id="1239387-172173508-1725" author="12336959@N00" raw="mexico">mexico</tag>
          <tag id="1239387-172173508-42092" author="12336959@N00" raw="rosarito">rosarito</tag>
          <tag id="1239387-172173508-69612" author="12336959@N00" raw="boozing">boozing</tag>
     </tags>
     <urls>
          <url type="photopage">http://www.flickr.com/photos/boozesandiego/123173508/</url>
     </urls>
</photo>
</string></value></param></params></methodResponse>

Ok and heres my code to parse it:

      dim flickrResponse, photoset, node, Nodes, NewNode
      
      dim xmlstr
      xmlstr = replace(replace(xmlHttp.ResponseXML.xml, "&lt;", "<"),"&gt;",">")

      Const XPATH = "methodResponse/params/param/value/string/photo"
      
      Set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
      'response.write "<div style='font-size:14px;'><xmp>" & xmlstr & "</xmp></div>"

      Function GetXmlValue(Node, ValueXPath)
            Dim NewNode
            
            Set NewNode = Node.selectSingleNode(ValueXPath)
            If Not NewNode Is Nothing Then
                  GetXmlValue = NewNode.Text
            Else
                  GetXmlValue = ""
            End If
      End Function



            If xmlDom.loadXML(xmlstr) Then
                  Set Nodes = xmlDom.selectNodes(XPATH)
                  If Not Nodes Is Nothing Then
                        For Each Node In Nodes %>
                              <div class="photoDetail">
                                    <img src="http://static.flickr.com/<%= node.getAttribute("server") %>/<%= node.getAttribute("id") %>_<%= node.getAttribute("secret") %>.jpg">
                                    <p><%= GetXmlValue(Node, "title")%></p>
                                    <p><%= GetXmlValue(Node, "description")%></p>
                                    <p>Image Hosted by <a href="<%= GetXmlValue(Node, "urls")%>">Flickr</a></p>
                              </div>
                  <%  Next
                  End If
            Else
                  Response.Write(xmlDom.parseError.reason)
            End If

-------------------

The problem i'm having is i'm not sure how to target different values and attibutes. Here i have managed to get values and attributes at the 'photo' level but not any lower. Also i need to loop through the tags and get each one separatly so i can make them a link
0
Comment
Question by:alex_wareing
1 Comment
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
Comment Utility
Try it like this:

Dim flickrResponse, photoset, Node, Nodes, TagNode, TagNodes

Dim xmlstr

xmlstr = replace(replace(xmlHttp.ResponseXML.xml, "&lt;", "<"),"&gt;",">")

Const XPATH = "methodResponse/params/param/value/string/photo"

Set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
'response.write "<div style='font-size:14px;'><xmp>" & xmlstr & "</xmp></div>"

If xmlDom.loadXML(xmlstr) Then
   Set Nodes = xmlDom.selectNodes(XPATH)
   If Not Nodes Is Nothing Then
        For Each Node In Nodes %>
             <div class="photoDetail">
                  <img src="http://static.flickr.com/<%= node.getAttribute("server") %>/<%= node.getAttribute("id") %>_<%= node.getAttribute("secret") %>.jpg">
                  <p><%= GetXmlValue(Node, "title")%></p>
                  <p><%= GetXmlValue(Node, "description")%></p>
                  <p>Image Hosted by <a href="<%= GetXmlValue(Node, "urls")%>">Flickr</a></p>
             </div>
            <%
            Set TagNodes = Node.selectNodes("tags/tag")
            If Not TagNodes Is Nothing Then
                        For Each TagNode In TagNodes
                              Response.Write GetXmlValue(TagNode, "@id")
                              Response.Write GetXmlValue(TagNode, "@author")
                              Response.Write GetXmlValue(TagNode, "@raw")
                              Response.Write GetXmlValue(TagNode, ".")
                        Next
                  End If
                  %>
   <%  Next
   End If
Else
   Response.Write(xmlDom.parseError.reason)
End If

Function GetXmlValue(Node, ValueXPath)
  Dim NewNode
 
  Set NewNode = Node.selectSingleNode(ValueXPath)
  If Not NewNode Is Nothing Then
       GetXmlValue = NewNode.Text
  Else
       GetXmlValue = ""
  End If
End Function

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Classic ASP + JS 4 67
JQuery Date Time picker not showing 29 92
Contact Forms for Website 6 65
Fixed div within Bootstrap carousel item 11 25
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now