Base de connaissance
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
Le Deal du moment :
Tablette 11″ Xiaomi- Mi Pad 6 global version ...
Voir le deal
224.97 €

asp & xml

Aller en bas

asp & xml Empty asp & xml

Message  Admin Mar 24 Fév - 11:54

Using ASP for XML files
I have some examples here of using ASP on XML files. The first example just displays a very small XML file.

ex1.xml

<?xml version="1.0"?>
<person>
<name>John Avitabile</name>
<phone>458-5317</phone>
<address>Albertus 418</address>
</person>

ex1.asp

<%
Option Explicit
Response.Buffer = True
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load (Server.MapPath("ex1.xml"))

Dim name, phone, address
name = xml.documentElement.childNodes(0).text
phone = xml.documentElement.childNodes(1).text
address = xml.documentElement.childNodes(2).text

Set xml = Nothing
%>
<html>
<head> <title>Example 1</title> </head>
<body>
<table>
<tr><th>Name</th><th>Phone</th><th>Address</th></tr>
<tr><td><% =name %></td><td><% =phone %></td><td><% =address %></td></tr>
</table>
</body>
</html>



--------------------------------------------------------------------------------

The second example will display the data from a larger file:

ex2.xml

<?xml version="1.0"?>
<people>
<person>
<name>Judith Avitabile</name>
<phone>498-1111</phone>
<address>10 Green Street</address></person>
<person>
<name>Jackie Avitabile</name>
<phone>238-4093</phone>
<address>2 Manning Ave</address>
</person>
</people>

ex3.asp

<%
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load(Server.MapPath("ex2.xml"))

Dim root
Set root = xml.documentElement

%>
<html>
<head> <title>Example 3</title> </head>
<body>
<h1> All People In File</h1>
<table border=1>
<tr><th>Name</th><th>Phone</th><th>Address</th></tr>
<% Dim nameNode, name, phone, address, thisChild
For I = 0 TO (root.childNodes.length - 1)
Set thisChild = root.childNodes(I)
name = thisChild.childNodes(0).Text
phone = thisChild.childNodes(1).Text
address = thisChild.childNodes(2).Text
%>
<tr><td><% =name %></td><td><% =phone %></td><td><% =address %></td></tr>
<% Next %>
</table>
</body>
</html>



--------------------------------------------------------------------------------

The next example will let you add a new person to the XML file:

ex2.htm

<html>
<head><title>Add a Person</title></head>
<body>
<h1>Add a Person</h1>
<form action="http://academic2.strose.edu/Math_And_Science/avitabij/aspxml/ex2.asp"
method="POST">
Name: <input type="text" name="name">
<br>Phone: <input type="text" name="phone">
<br>Address: <input type="text" name="address">
<br><input type="submit" value="Add the Name">
</form>
</body>

ex2.asp

<%
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load(Server.MapPath("ex2.xml"))

Dim name, phone, address, newperson
name = Request("name")
phone = Request("phone")
address = Request("address")

Dim root
Set root = xml.documentElement

Set newperson = xml.createNode("element", "person", "")

Dim newname, newphone, newaddress
Set newname = xml.createNode("element", "name", "")
newname.text = name
Set newphone = xml.createNode("element", "phone", "")
newphone.text = phone
Set newaddress = xml.createNode("element", "address", "")
newaddress.text = address

newperson.appendChild(newname)
newperson.appendChild(newphone)
newperson.appendChild(newaddress)

root.appendChild(newperson)

xml.save(Server.Mappath("ex2.xml"))
%>
<html>
<head> <title>Example 1</title> </head>
<body>
<h1> Person Added </h1>
<table>
<tr><th>Name</th><th>Phone</th><th>Address</th></tr>
<tr><td><% =name %></td><td><% =phone %></td><td><% =address %></td></tr>
</table>
</body>
</html>



--------------------------------------------------------------------------------

The next example will let you remove a node from the XML document:

ex4.htm

<html>
<head><title>Remove a Person</title></head>
<body>
<h1>Remove a Person</h1>
<form action="http://academic2.strose.edu/Math_And_Science/avitabij/aspxml/ex4.asp"
method="POST">
Name: <input type="text" name="name">
<br><input type="submit" value="Remove">
</form>
</body>

ex4.asp

<%
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load(Server.MapPath("ex2.xml"))

Dim searchname
searchname = Request("name")

Dim root, name, thisChild, found
Set root = xml.documentElement
found = False
For I = (root.childNodes.length - 1) TO 0 STEP -1
Set thisChild = root.childNodes(I)
name = thisChild.childNodes(0).Text
If name = searchname Then
root.removeChild(thisChild)
found = True
End If
Next
xml.save(Server.Mappath("ex2.xml"))
%>
<html>
<head> <title>Example 4</title> </head>
<body>
<% If found = true Then %>
<h1> <% =searchname %> Removed </h1>
<% Else %>
<h1> <% =searchname %> Not Found </h1>
<% End If %>
</body>
</html>



--------------------------------------------------------------------------------

The last example will let the user change the data on a person (after they have been found).

ex5.htm

<html>
<head><title>Change a Property</title></head>
<body>
<h1>Change a Person</h1>
<form action="http://academic2.strose.edu/Math_And_Science/avitabij/aspxml/ex5.asp"
method="POST">
Name: <input type="text" name="name">
<br><input type="submit" value="Look Up">
</form>
</body>

ex5.asp (returns the information found)

<%
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load(Server.MapPath("ex2.xml"))

Dim searchname
searchname = Request("name")

Dim root, name, address, phone, thisChild, found
Set root = xml.documentElement
%>
<html>
<head> <title>Example 5</title> </head>
<body>
<%
found = False
For I = (root.childNodes.length - 1) TO 0 STEP -1
Set thisChild = root.childNodes(I)
name = thisChild.childNodes(0).Text
phone = thisChild.childNodes(1).Text
address = thisChild.childNodes(2).Text
If name = searchname Then %>
<h1> <% =searchname %> Found </h1>
<form action="http://phantom.strose.edu/academic/avitabij/aspxml/ex6.asp"
method="POST">
Name: <input type="text" name="name" value ="<% =name %>">
<br>Address: <input type="text" name="phone" value ="<% =phone %>">
<br>Name: <input type="text" name="address" value ="<% =address %>">
<input type="hidden" name="original" value ="<% =name %>">
<br><input type="submit" value="Change">
</form>
<% found = True
End If
Next
If found = False Then %>
<h1> <% =searchname %> Not Found </h1>
<% End If %>
</body>
</html>


ex6.asp

<%
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load(Server.MapPath("ex2.xml"))

Dim original, name, address, phone
original = Request("original")
name = Request("name")
address = Request("address")
phone = Request("phone")

Dim root, thisName, thisChild, found
Set root = xml.documentElement
For I = (root.childNodes.length - 1) TO 0 STEP -1
Set thisChild = root.childNodes(I)
ThisName = thisChild.childNodes(0).Text
If ThisName = original Then
thisChild.childNodes(0).Text = name
thisChild.childNodes(1).Text = phone
thisChild.childNodes(2).Text = address
End If
Next
xml.save(Server.Mappath("ex2.xml"))
%>
<html>
<head> <title>Example 6</title> </head>
<body>
<h1> All People In File</h1>
<table border=1>
<tr><th>Name</th><th>Phone</th><th>Address</th></tr>
<% Dim nameNode, name1, phone1, address1, thisChild1
For I = 0 TO (root.childNodes.length - 1)
Set thisChild1 = root.childNodes(I)
name1 = thisChild1.childNodes(0).Text
phone1 = thisChild1.childNodes(1).Text
address1 = thisChild1.childNodes(2).Text
%>
<tr><td><% =name1 %></td><td><% =phone1 %></td><td><% =address1 %></td></tr>
<% Next %>
</table>
</body>
</html>

Admin
Admin

Nombre de messages : 45
Date d'inscription : 20/01/2009

http://programming.formyjob.net

Revenir en haut Aller en bas

Revenir en haut


 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum