/ Forside / Teknologi / Udvikling / ASP / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
ASP
#NavnPoint
smorch 9259
Harlekin 1866
molokyle 1040
Steffanst.. 758
gandalf 657
smilly 564
gibson 560
cumano 530
MouseKeep.. 480
10  Random 410
Skrive til xml fil
Fra : Morten Jorgensen


Dato : 23-08-06 22:10

Hejsa!

Jeg har her et lille script der skriver til en xml fil. Problemet
er bare at den laver nye childNodes hver gang. Jeg vil gerne have
det sådan at den kun opdatere dem i stedet for at lave nye.
Hvordan gør jeg det i dette script?

<%@ Language=JScript%>
<%Server.ScriptTimeout=21478836%>
<%Response.Buffer=false%>
<%
   // 1) To check whether the user has already pressed the submit
button
   var submit   = Request.Form("submit").Count;
   if( submit > 0 ){
   
      // 2) Create the needed to variables to store data from the
form   
      var name   = Request.Form("Name");
      var age      = Request.Form("Age");
      var gender   = Request.Form("Gender");
      var pcode   = Request.Form("PostalCode");
      
      // 3) First check whether the user has entered anything or not
!
      var error = "";
      if ( name == "" )
         error = "Name ";
      if ( age == "" )
         error += "Age ";
      if ( pcode == "")
         error += "PostalCode ";
         
      // 4) We have found that the user didnt entered anything
      if(error!=""){
         Response.Write("<center>");
         Response.Write("<font fac=verdana size=2>");
         Response.Write("Please enter the following data:<br>");
         Response.Write("<b>");
         Response.Write(error);
         Response.Write("</b>");
         Response.Write("</font>");
         Response.Write("</center>");
      }
      else{
         // Response.Write(name+" has been added to your Friend
list.");
         // 5) Load the xmlDoc and create the required
elements/nodes         
         var
xmlDoc=Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
         xmlDoc.async="false";
         xmlDoc.load(Server.MapPath("Person.xml"));
   
         var nodeList = xmlDoc.getElementsByTagName("PersonList");
         if(nodeList.length > 0){
            var parentNode = nodeList(0) ;
            var personNode = xmlDoc.createElement("Person");
            var nameNode = xmlDoc.createElement("Name");
            var ageNode = xmlDoc.createElement("Age");
            var genderNode = xmlDoc.createElement("Gender");
            var pcodeNode = xmlDoc.createElement("PostalCode");
         
            nameNode.text = name;
            ageNode.text = age;
            genderNode.text= gender;
            pcodeNode.text = pcode;   
            
            // 6) Attach the nodes to the parent node (Person)
            parentNode.appendChild(personNode);
            personNode.appendChild(nameNode);
            personNode.appendChild(ageNode);
            personNode.appendChild(genderNode);
            personNode.appendChild(pcodeNode);
            
            // 7) Now save the nodes to the file
            xmlDoc.save(Server.MapPath("Person.xml"));
         }
      }
   }
%>   
<html><body>
<CENTER><p><FONT face=Verdana><U><b>Add My Friends to XML</b></U>
</FONT> </p></CENTER>
<form action="VerifyPerson.asp" method="post" name="frmPerson"
id="frmPerson">
<table border=0 align=center style="height: 184px" width="255">
<TBODY>
<TR><TD width="78"><FONT face=Verdana size=2>
<P align=left>Name:</FONT></P></TD><TD width="154"><INPUT
name=Name ><FONT
face=Verdana size=2></FONT></TD>
<TR><TD width="78"><FONT face=Verdana size=2>Age: </FONT>
</TD><TD width="154"><INPUT name=Age ><FONT
face=Verdana size=2></FONT></TD>
<TR><TD width="78"><FONT face=Verdana
size=2>Gender:</FONT></TD><TD width="154">
<SELECT style="WIDTH: 154px" name=Gender>
<OPTION value=Male selected>Male</OPTION>
<OPTION value=Female>Female</OPTION>
</SELECT>
</TD>
<TR><TD width="78"><FONT face=Verdana size=2>Postalcode: </FONT>
</TD><TD width="154"><INPUT name=Postalcode ><FONT
face=Verdana></FONT></TD>
<td width="3">
<P><FONT face=Verdana></FONT></P></TR>
<td width="78">
<P><FONT face=Verdana></FONT></P>
<TR>
<TD COLSPAN=2 width="238"><center><INPUT type=submit
value=Submit name=submit>
<INPUT type=reset value=Reset name=reset></center>
</form>
</TABLE><BR><br>
<CENTER><FONT face=Verdana><U><b>My Friend list</b></U> </FONT>
</CENTER><br>
<table border="1" style="border-collapse: collapse; height:
20px" bordercolor=#000000 align=center bgColor=silver
width="417">

<TR><TD width="93"><FONT face=Verdana size=2>
<STRONG style="BACKGROUND-COLOR:
silver">Name</STRONG></FONT></TD>
<TD width="69"><FONT face=Verdana size=2><STRONG
style="BACKGROUND-COLOR: silver">Age</STRONG></FONT></TD>
<TD width="112"><FONT face=Verdana size=2><STRONG
style="BACKGROUND-COLOR: silver">Gender</STRONG></FONT></TD>
<TD width="115"><FONT face=Verdana size=2><STRONG
style="BACKGROUND-COLOR: silver">Postal Code</STRONG></FONT></TD>
</TR>
<%
   // This part is used to display the data in a table via XSL
   
   var objXMLDoc =
Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
   objXMLDoc.async = false;
   objXMLDoc.load(Server.MapPath("person.xml"));
   
   var xsl=Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
   xsl.async = false;
   xsl.load(Server.MapPath("person.xsl"));

   var xmlQuery="//Person";
   var
docHeadlines=objXMLDoc.documentElement.selectNodes(xmlQuery);
   
   var numNodes;
   numNodes=docHeadlines.length;

   var nn;
   for(var i=0;i<numNodes;i++){
      nn = docHeadlines.nextNode();
      Response.Write(nn.transformNode(xsl));
   }
%>
</table>
</body>
</html>

--
Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
- Pædagogiske tutorials på dansk
- Kom godt i gang med koderne
KLIK HER! => http://www.html.dk/tutorials

 
 
Morten Jorgensen (25-08-2006)
Kommentar
Fra : Morten Jorgensen


Dato : 25-08-06 08:17

Slet ingen der kan fortælle mig hvordan jeg får den til at opdatere
i stedet for at skrive nye childs hver gang?



Morten Jorgensen wrote in dk.edb.internet.webdesign.serverside.asp:
> Hejsa!
>
> Jeg har her et lille script der skriver til en xml fil. Problemet
> er bare at den laver nye childNodes hver gang. Jeg vil gerne have
> det sådan at den kun opdatere dem i stedet for at lave nye.
> Hvordan gør jeg det i dette script?
>
> <%@ Language=JScript%>
> <%Server.ScriptTimeout=21478836%>
> <%Response.Buffer=false%>
> <%
&gt;    // 1) To check whether the user has already pressed the submit
&gt; button
&gt;    var submit   = Request.Form("submit").Count;
&gt;    if( submit > 0 ){
&gt;    
&gt;       // 2) Create the needed to variables to store data from the
&gt; form   
&gt;       var name   = Request.Form("Name");
&gt;       var age      = Request.Form("Age");
&gt;       var gender   = Request.Form("Gender");
&gt;       var pcode   = Request.Form("PostalCode");
&gt;       
&gt;       // 3) First check whether the user has entered anything or
not
&gt; !
&gt;       var error = "";
&gt;       if ( name == "" )
&gt;          error = "Name ";
&gt;       if ( age == "" )
&gt;          error += "Age ";
&gt;       if ( pcode == "")
&gt;          error += "PostalCode ";
&gt;          
&gt;       // 4) We have found that the user didnt entered anything
&gt;       if(error!=""){
&gt;          Response.Write("<center>");
&gt;          Response.Write("<font fac=verdana size=2>");
&gt;          Response.Write("Please enter the following data:<br>");
&gt;          Response.Write("<b>");
&gt;          Response.Write(error);
&gt;          Response.Write("</b>");
&gt;          Response.Write("</font>");
&gt;          Response.Write("</center>");
&gt;       }
&gt;       else{
&gt;          // Response.Write(name+" has been added to your Friend
&gt; list.");
&gt;          // 5) Load the xmlDoc and create the required
&gt; elements/nodes         
&gt;          var
&gt; xmlDoc=Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
&gt;          xmlDoc.async="false";
&gt;          xmlDoc.load(Server.MapPath("Person.xml"));
&gt;    
&gt;          var nodeList = xmlDoc.getElementsByTagName("PersonList");
&gt;          if(nodeList.length > 0){
&gt;             var parentNode = nodeList(0) ;
&gt;             var personNode = xmlDoc.createElement("Person");
&gt;             var nameNode = xmlDoc.createElement("Name");
&gt;             var ageNode = xmlDoc.createElement("Age");
&gt;             var genderNode = xmlDoc.createElement("Gender");
&gt;             var pcodeNode = xmlDoc.createElement("PostalCode");
&gt;          
&gt;             nameNode.text = name;
&gt;             ageNode.text = age;
&gt;             genderNode.text= gender;
&gt;             pcodeNode.text = pcode;   
&gt;             
&gt;             // 6) Attach the nodes to the parent node (Person)
&gt;             parentNode.appendChild(personNode);
&gt;             personNode.appendChild(nameNode);
&gt;             personNode.appendChild(ageNode);
&gt;             personNode.appendChild(genderNode);
&gt;             personNode.appendChild(pcodeNode);
&gt;             
&gt;             // 7) Now save the nodes to the file
&gt;             xmlDoc.save(Server.MapPath("Person.xml"));
&gt;          }
&gt;       }
&gt;    }
&gt; %>   
> <html><body>
> <CENTER><p><FONT face=Verdana><U><b>Add My Friends to XML</b></U>
> </FONT> </p></CENTER>
> <form action="VerifyPerson.asp" method="post" name="frmPerson"
> id="frmPerson">
> <table border=0 align=center style="height: 184px" width="255">
> <TBODY>
> <TR><TD width="78"><FONT face=Verdana size=2>
> <P align=left>Name:</FONT></P></TD><TD width="154"><INPUT
> name=Name ><FONT
> face=Verdana size=2></FONT></TD>
> <TR><TD width="78"><FONT face=Verdana size=2>Age: </FONT>
> </TD><TD width="154"><INPUT name=Age ><FONT
> face=Verdana size=2></FONT></TD>
> <TR><TD width="78"><FONT face=Verdana
> size=2>Gender:</FONT></TD><TD width="154">
> <SELECT style="WIDTH: 154px" name=Gender>
> <OPTION value=Male selected>Male</OPTION>
> <OPTION value=Female>Female</OPTION>
> </SELECT>
> </TD>
> <TR><TD width="78"><FONT face=Verdana size=2>Postalcode: </FONT>
> </TD><TD width="154"><INPUT name=Postalcode ><FONT
> face=Verdana></FONT></TD>
> <td width="3">
> <P><FONT face=Verdana></FONT></P></TR>
> <td width="78">
> <P><FONT face=Verdana></FONT></P>
> <TR>
> <TD COLSPAN=2 width="238"><center><INPUT type=submit
> value=Submit name=submit>
> <INPUT type=reset value=Reset name=reset></center>
> </form>
> </TABLE><BR><br>
> <CENTER><FONT face=Verdana><U><b>My Friend list</b></U> </FONT>
> </CENTER><br>
> <table border="1" style="border-collapse: collapse; height:
> 20px" bordercolor=#000000 align=center bgColor=silver
> width="417">
>
> <TR><TD width="93"><FONT face=Verdana size=2>
> <STRONG style="BACKGROUND-COLOR:
> silver">Name</STRONG></FONT></TD>
> <TD width="69"><FONT face=Verdana size=2><STRONG
> style="BACKGROUND-COLOR: silver">Age</STRONG></FONT></TD>
> <TD width="112"><FONT face=Verdana size=2><STRONG
> style="BACKGROUND-COLOR: silver">Gender</STRONG></FONT></TD>
> <TD width="115"><FONT face=Verdana size=2><STRONG
> style="BACKGROUND-COLOR: silver">Postal Code</STRONG></FONT></TD>
> </TR>
> <%
&gt;    // This part is used to display the data in a table via XSL
&gt;    
&gt;    var objXMLDoc =
&gt; Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
&gt;    objXMLDoc.async = false;
&gt;    objXMLDoc.load(Server.MapPath("person.xml"));
&gt;    
&gt;    var xsl=Server.CreateObject("MICROSOFT.FreeThreadedXMLDOM");
&gt;    xsl.async = false;
&gt;    xsl.load(Server.MapPath("person.xsl"));
&gt;
&gt;    var xmlQuery="//Person";
&gt;    var
&gt; docHeadlines=objXMLDoc.documentElement.selectNodes(xmlQuery);
&gt;    
&gt;    var numNodes;
&gt;    numNodes=docHeadlines.length;
&gt;
&gt;    var nn;
&gt;    for(var i=0;i<numNodes;i++){
&gt;       nn = docHeadlines.nextNode();
&gt;       Response.Write(nn.transformNode(xsl));
&gt;    }
&gt; %>
> </table>
> </body>
> </html>
>
> --
> Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
> - Pædagogiske tutorials på dansk
> - Kom godt i gang med koderne
> KLIK HER! => http://www.html.dk/tutorials


--
Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
- Pædagogiske tutorials på dansk
- Kom godt i gang med koderne
KLIK HER! => http://www.html.dk/tutorials

Søg
Reklame
Statistik
Spørgsmål : 177558
Tips : 31968
Nyheder : 719565
Indlæg : 6408925
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste