[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

What is the precise childnode-path to loop xml-file?

<%
for i = 0 to xmlcontents.ChildNodes.attributes.length - 1
set xmlcontent = xmlcontents.ChildNodes(i).attributes %>

this code is my attempt to convert another fine working looping to this need (see attached snippet of the original code).

I want to present some xml-attributes as records, but I cant quite figure out how to loop through the xml-file, that looks like this:
 
<map>
        <positions>
                <position id="1" xPosition="80" yPosition="80" xWidth="120" yHeight="60">Bruxelles</position>      
                <position id="2" xPosition="10" yPosition="60" xWidth="100" yHeight="90">New York</position>
                <position id="3" xPosition="100" yPosition="180" xWidth="200" yHeight="20">Los Angeles</position>
                <position id="4" xPosition="150" yPosition="150" xWidth="120" yHeight="30">Singapore</position>
        </positions>
</map>

I have tried to change a code that was doing the job with a xml-file that was formatted with nodeelements ( see attached snippet).
The purpose is to present records which can be edited and deleted.

<%
dim objXML
set objXML = server.CreateObject("Microsoft.FreeThreadedXMLDOM")

if request.QueryString("delete") <> "" then
objXML.Load(xmlpath)
set remove = _
objXML.childnodes(0).childnodes(1).childnodes(request.QueryString("delete"))
objXML.childnodes(0).childnodes(1).removeChild(remove)
objXML.save(xmlpath)
end if

objXML.Load(xmlpath)
set xmlcontents = objXML.DocumentElement.selectSingleNode("positions")
%>

<html>
<body>
  <table width="180">
    <tr>
      <th width="80">Options</th>
      <th width="20">id</th>
      <th width="20">xPosition</th>
      <th width="20">yPosition</th>
      <th width="20">xWidth</th>
      <th width="20">yHeight</th>
         </tr>

'The solution I want is solving the code below.

    <%
for i = 0 to xmlcontents.ChildNodes.attributes.length - 1
set xmlcontent = xmlcontents.ChildNodes(i).attributes %>
    <tr>
      <td><a href="editcontent.asp?id=<%= i+1%>">Edit</a> - <a href="?delete=<%= i%>">Delete</a></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.id %></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.xPosition %></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.yPosition %></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.xWidth %></td>
      <td><%= xmlcontent.ChildNodes(i).attributes.yWidth %></td>
     </tr>
    <% next %>
   </table>
  </body>
</html>

I have to stress that Im not interested in doing it any other way. I want the rest of the code to be precisely as it is. No VB-script solution or any other ways.
Just make the topcode usable to the script as the codesnippet does to its xml-file.

<%
for i = 0 to xmlcontents.ChildNodes.length - 1 
set xmlcontent = xmlcontents.ChildNodes.item(i)
%>
    <tr> 
      <td><a href="editcontent.asp?id=<%= i+1%>">Edit</a> - <a href="?delete=<%= i%>">Delete</a></td>
      <td><%= xmlcontent.ChildNodes.item(1).text%></td>
      <td><%= xmlcontent.ChildNodes.item(2).text%></td>
      <td><%= xmlcontent.ChildNodes.item(3).text%></td>
      <td><%= xmlcontent.ChildNodes.item(4).text%></td>
      <td><%= xmlcontent.ChildNodes.item(5).text%></td>
    </tr>
    <% next %>

Open in new window

0
SimonAdrian
Asked:
SimonAdrian
  • 3
  • 2
1 Solution
 
hieloCommented:
>> No VB-script solution or any other ways
If you don't want vbscript, then what is this?

for i = 0 to xmlcontents.ChildNodes.length - 1
...
next
0
 
Anthony PerkinsCommented:
>>No VB-script solution or any other ways.<<
Than why post it here?  Perhaps you should post it here: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/#browseZones
0
 
SimonAdrianAuthor Commented:
Sorry, I just want to stress, that I  want the path changed, not the entire script.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
hieloCommented:
Here you go:
<%@language="vbscript"%>
<%
Dim xmlPath
xmlPath = Server.MapPath("map.xml")
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.async = False
'objXMLDoc.load( Server.MapPath("map.xml"))
 
if request.QueryString("delete") <> "" then
	objXML.Load(xmlpath)
	set remove = _
	objXML.documentElement.childnodes(0).childnodes(request.QueryString("delete"))
	objXML.documentElement.childnodes(0).removeChild(remove)
	objXML.save(xmlpath)
end if
objXML.Load(xmlpath)
 
 
set xmlcontents = objXML.documentElement.selectSingleNode("positions")
Response.Write xmlcontents.childNodes.length
 
'Response.End
Dim limit
limit = xmlcontents.childNodes.length - 1
%>
<table border='1'>
<%
for i = 0 to  limit
	set xmlcontent = xmlcontents.ChildNodes.item(i).attributes
%>
    <tr> 
      <td><a href="editcontent.asp?id=<%= i+1%>">Edit</a> - <a href="?delete=<%= i%>">Delete</a></td>
      <td><%= xmlcontent(0).text%></td>
      <td><%= xmlcontent(1).text%></td>
      <td><%= xmlcontent(2).text%></td>
      <td><%= xmlcontent(3).text%></td>
      <td><%= xmlcontent(4).text%></td>
    </tr>
    <% next %>
</table>

Open in new window

0
 
hieloCommented:
Update: You don't need this:
Response.Write xmlcontents.childNodes.length

(I used it for debugging)
0
 
SimonAdrianAuthor Commented:
Im gratefull, thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now