Solved

Targetting attributes and values in XML

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
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 how to dynamically set the form action using jQuery.

776 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