top of page

asp add, delete, modify node in xml file

  • thistedle50ribdgl
  • Jun 12, 2020
  • 4 min read

1.ASP displays all nodes within the xml file


2.ASP add node to xml file


three. ASP deletes a node in the xml file


four. Use asp to modify nodes in xml




1.ASP operation xml--display all nodes in xml file


Normally use a database to save information, but you'll be able to also use xml files to save data. Assume that the content of the xml file is:


test.xml


? xmlversion = '1.0' encoding = 'gb2312'?


web site


root


list


0 / id


name / name


urlhttp: //www.sina.com.cn/url


time2007-11-110: 10: ten / time


/ list


list


1 / id


namegoogle / name


urlhttp: //www.google.cn/url


time2007-11-211: 11: 11 / time


/ list


/ root


/ web site


Asp file applied to show the content material in xml


index.asp


style


body font-size: 12px;


/ style


%


strSourceFile = Server.MapPath ('./') '\\ test.xml'


'' The path to obtain the XML file is diverse according to your virtual directory


SetobjXML = Server.CreateObject ('Microsoft.FreeThreadedXMLDOM')


'' Develop an XML object with free of charge thread


objXML.load (strSourceFile) '' Read XML file into memory


SetobjRootsite = objXML.documentElement.selectSingleNode ('root')


'' Pick root node


num = objRootsite.childNodes.length-1


Fori = 0Tonum


response.create 'site name:' objRootsite.childNodes.item (i) .childNodes.item (1) .text 'ahref =' 'javascript: delxml (' objRootsite.childNodes.item (i) .childNodes.item (0) .text ')' '/ aahref =' 'editxml.asp? id =' objRootsite.childNodes.item (i) .childNodes.item (0) .text '' '/ a


'


response.create 'URL:' objRootsite.childNodes.item (i) .childNodes.item (2) .text '


'


response.write 'Add time:' objRootsite.childNodes.item (i) .childNodes.item (three) .text '


'


next


%


script


functiondelxml (id)




if (! confirm ('Are you sure you want to delete?')) returnfalse;


location.href = '' delxml.asp? id = '' + id;




/ script


Note: index.asp really should be placed inside the exact same directory because the test.xml file.


two.ASP operation xml--add node to xml file


! DOCTYPEHTMLPUBLIC '-// W3C // DTDHTML4.0Transitional // EN'


HTML


HEAD


TITLE adds new node / TITLE to xml


METANAME = 'Generator' Content = 'EditPlus'


METANAME = 'Author' Content material = ''


METANAME = 'Keywords' Content = ''


METANAME = 'Description' Content material = ''


style


physique font-size: 12px;


/ style


/ HEAD


Physique


!-# includefile = 'top.asp'-


%


addok = request ('addok')


Ifaddok = '' OrIsNull (addok) Then


%


formname = 'form1' process = 'post' action = '? addok = add'


Web page name: inputtype = 'text' name = 'site' size = '30'


Web page: inputtype = 'text' name = 'url' size = '40'


inputtype = 'submit' value = 'Submit' inputtype = 'reset' worth = 'Reset'


/ kind


%


EndIf


Ifaddok = 'add' then


site = request ('site')


url = request ('url')


strSourceFile = Server.MapPath ('./') '\\ test.xml'


'' The path to get the XML file is various according to your virtual directory


SetobjXML = Server.CreateObject ('Microsoft.XMLDOM')


'' Make XML Object


objXML.load (strSourceFile)


'' Study XML file into memory


SetobjRootlist = objXML.documentElement.selectSingleNode ('root')


'' Choose root node


ifobjRootlist.hasChildNodesthen


'' Judge whether or not NewList has child nodes (for the reason that each time root has no kid nodes, if it is not judged, it will report an error when it is shipped for the initial time.


id = objRootlist.lastchild.firstchild.text + 1


'' Right here could be the ID number of the kid node to be inserted, whose ID quantity is the initial child node (firstchild) of your last kid node (lastchild) of root


'' ID quantity increases by 1 (right here we raise as outlined by the ID number of the relational database)


else


'' If there's no word sub-node, it truly is the initial message ID quantity is set to 1


id = 1


endIf


brstr = chr (13) chr (ten) chr (9)


XMLnode = brstrlistvbnewline_


'' id / idvbnewline_


'namesite / namevbnewline_


'urlurl / urlvbnewline_


'timeReplace (Replace (Now (),' AM ',' '),' PM ',' ') / timevbnewline_


'/ listvbnewlinevbnewline


'' Generate XML fragments depending on the obtained data


setobjXML2 = Server.CreateObject ('Microsoft.XMLDOM')


'' Make a brand new XML object


objXML2.loadXML (XMLnode)


'' Study the XML version fragment into memory


setrootNewNode = objXML2.documentElement


'' Get the root node of objXML2


objRootlist.appendChild (rootNewNode)


'' Insert the XML fragment into test.xml


objXML.save (strSourceFile)


'' Shop test.xml file (since test.xml isn't stored, only updated in memory)


setobjXML = nothing at all


setobjXML2 = practically nothing


Response.Redirect ('index.asp')


response.end


EndIf


%


/ Physique


/ HTML


The content material in test.xml is


? xmlversion = '1.0' encoding = 'gb2312'?


web-site


root


list


0 / id


name / name


urlhttp: //www.sina.com.cn/url


time2007-11-110: ten: ten / time


/ list


/ root


/ web site


3. ASP operation xml-delete a node within the xml file


detail.asp? id = 157 This web page is utilised to display each of the nodes inside the xml file. http://sqworl.com/dbmzmt 'll find two connections for deletion and editing next to each node.


%


id = request ('id')


ifIsNumeric (id) = falseorisNull (id) then


response.write ('The parameter is incorrect, please return!')



endif


strSourceFile = Server.MapPath ('./') '\\ test.xml'


'' The path to have the XML file is different as outlined by your virtual directory


SetobjXML = Server.CreateObject ('Microsoft.XMLDOM')


'' Make XML Object


objXML.load (strSourceFile)


'' Study XML file into memory


SetobjRootlist = objXML.documentElement.selectSingleNode ('root')


setrootOldNode = objRootlist.childNodes.item (id)


objRootlist.RemoveChildrootOldNode


'' Delete Node


objXML.save (strSourceFile)


'' Store test.xml file (for the reason that test.xml is not stored, only updated in memory)


setobjXML = Nothing


setobjXML2 = Nothing at all


Response.Redirect ('index.asp')


response.finish


%


four. ASP operation xml-modify the node in the xml file


! DOCTYPEHTMLPUBLIC '-// W3C // DTDHTML4.0Transitional // EN'


HTML


HEAD


TITLE modify xml content material / TITLE


METANAME = 'Generator' Content material = 'EditPlus'


METANAME = 'Author' Content = ''


METANAME = 'Keywords' Content = ''


METANAME = 'Description' Content = ''


/ HEAD


Physique


!-# includefile = 'top.asp'-


%


id = request ('id')


ifIsNumeric (id) = falseorisNull (id) then


response.write ('The parameter is incorrect, please return!')


response.end


endif


addok = request ('addok')


Ifaddok = '' OrIsNull (addok) then


strSourceFile = Server.MapPath ('./') '\\ test.xml'


'' The path to have the XML file is various according to your virtual directory


SetobjXML = Server.CreateObject ('Microsoft.FreeThreadedXMLDOM')


'' Make an XML object with absolutely free thread


objXML.load (strSourceFile) '' Read XML file into memory


SetobjRootsite = objXML.documentElement.selectSingleNode ('root')


id = objRootsite.childNodes.item (id) .childNodes.item (0) .text


site = objRootsite.childNodes.item (id) .childNodes.item (1) .text


url = objRootsite.childNodes.item (id) .childNodes.item (2) .text


%


formname = 'form1' approach = 'post' action = '? addok = editid =% = id%


Website name: inputtype = 'text' name = 'site' value = '% = site%' size = '30'


Web-site: inputtype = 'text' name = 'url' value = '% = url%' size = '40'


inputtype = 'submit' worth = 'modify' inputtype = 'reset' value = 'reset'


/ form


%


EndIf


Ifaddok = 'edit' Then


web site = request ('site')


url = request ('url')


strSourceFile = Server.MapPath ('./') '\\ test.xml'


'' The path to get the XML file is diverse based on your virtual directory


SetobjXML = Server.CreateObject ('Microsoft.XMLDOM')


'' Develop XML Object


objXML.load (strSourceFile)


'' Study XML file into memory


SetobjRootlist = objXML.documentElement.selectSingleNode ('root')


setrootOldNode = objRootlist.childNodes.item (id)


brstr = chr (13) chr (10) chr (9)


XMLnode = brstrlistvbnewline_


'' id / idvbnewline_


'namesite / namevbnewline_


'urlurl / urlvbnewline_


'timeReplace (Replace (Now (),' AM ',' '),' PM ',' ') / timevbnewline_


'/ listvbnewlinevbnewline


'' Build XML fragments determined by the obtained information


setobjXML2 = Server.CreateObject ('Microsoft.XMLDOM')


'' Make a new XML object


objXML2.loadXML (XMLnode)


'' Read the XML version fragment into memory


setrootNewNode = objXML2.documentElement


'' Get the root node of objXML2


objRootlist.ReplaceChildrootNewNode, rootOldNode


'' Modify test.xml


objXML.save (strSourceFile)


'' Retailer test.xml file (because test.xml is not stored, only updated in memory)


setobjXML = absolutely nothing


setobjXML2 = nothing at all


Response.Redirect ('index.asp')


response.finish


Endif


%


/ Body


/ HTML

 
 
 

Recent Posts

See All

Comments


Subscribe to BrainStorm newsletter

I'm a title. ​Click here to edit me.

Thanks for submitting!

  • Twitter
  • Facebook
  • Linkedin

© 2023 by BrainStorm. Proudly created with Wix.com

bottom of page