Solved

Targetting attributes and values in XML

Posted on 2006-06-27
1
797 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

691 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