Solved

Targetting attributes and values in XML

Posted on 2006-06-27
1
792 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lines Not Lining Up 2 63
use of cookies and regulations 1 77
cookies analysis tools 2 73
How to write an English digest paper 6 24
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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

864 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

24 Experts available now in Live!

Get 1:1 Help Now