Solved

Targetting attributes and values in XML

Posted on 2006-06-27
1
795 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
ID: 16997386
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logiā€¦
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

827 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