Prev: avoiding error?
Next: sendmail finnishing
From: abruton on 30 Oct 2009 10:50 Hi I am trying to update a child node of an XML file IE Changing the value. The XML file looks like this: <user> <firstname>Andre</firstname> <lastname>Bruton</lastname> </user> Here is my Classic asp code: users_firstname = "Tristan" 'New code to put in the XML file Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument") xmlObj.async = False xmlObj.setProperty "ServerHTTPRequest", True xmlObj.Load(cURL) If xmlObj.parseError.errorCode <> 0 Then Response.Write "Error Reaqding File - " & xmlObj.parseError.reason & "<p>" End If Set xmlList = xmlObj.getElementsByTagName("user") For Each xmlItem In xmlList For Each xmlItem2 In xmlItem.childNodes a = xmlItem2.nodeName if a = "firstname" then firstname = xmlItem2.text if a = "lastname" then lastname = xmlItem2.text Next Next If firstname <> users_firstname Then Set nodeBook = xmlObj.selectSingleNode("//firstname") nodeBook.setAttribute "firstname", users_firstname Response.Write nodeBook.getAttribute("firstname") xmlObj.save(cDir & cFile) End If Set xmlObj = Nothing The problem is that it adds a new section to the XML file instead of updating the value of firstname from Andre to Tristan. The XML looks like this: <user> <firstname firstname="Andre6">Andre</firstname> <lastname>Bruton</lastname> </user> What is should look like is: <user> <firstname>Tristan</firstname> <lastname>Bruton</lastname> </user> Any idea how I can fix this? Best regards Andre
From: Bob Barrows on 30 Oct 2009 11:36 abruton wrote: > Hi > > I am trying to update a child node of an XML file IE Changing the > value. > > The XML file looks like this: > > <user> > <firstname>Andre</firstname> > <lastname>Bruton</lastname> > </user> > > > Here is my Classic asp code: > > users_firstname = "Tristan" 'New code to put in the XML file > > Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument") > xmlObj.async = False > xmlObj.setProperty "ServerHTTPRequest", True > xmlObj.Load(cURL) > If xmlObj.parseError.errorCode <> 0 Then > Response.Write "Error Reaqding File - " & xmlObj.parseError.reason > & "<p>" > End If > > Set xmlList = xmlObj.getElementsByTagName("user") > For Each xmlItem In xmlList > For Each xmlItem2 In xmlItem.childNodes > a = xmlItem2.nodeName > if a = "firstname" then firstname = xmlItem2.text > if a = "lastname" then lastname = xmlItem2.text > Next > Next If you only have a single <user> element, there is no need to do a loop: just use selectSingleNode: set usernode = xmlObj.selectSingleNode("//user") > > If firstname <> users_firstname Then > Set nodeBook = xmlObj.selectSingleNode("//firstname") > nodeBook.setAttribute "firstname", users_firstname > Response.Write nodeBook.getAttribute("firstname") > xmlObj.save(cDir & cFile) > End If > > Set xmlObj = Nothing > > > The problem is that it adds a new section to the XML file instead of > updating the value of firstname from Andre to Tristan. The XML looks > like this: > > <user> > <firstname firstname="Andre6">Andre</firstname> > <lastname>Bruton</lastname> > </user> > > > What is should look like is: > > <user> > <firstname>Tristan</firstname> > <lastname>Bruton</lastname> > </user> > > > Any idea how I can fix this? > Don't use getAttribute or SetAttribute. This is an attribute: firstname="Andre6" This is an element: <firstname>Tristan</firstname> Untested air code: set fnamenode = xmlObj.selectSingleNode("//user/firstname") if fnamenode.nodeValue <> users_firstname then fnamenode.nodeValue = users_firstname end if Response.Write fnamenode.nodeValue xmlObj.save cDir & cFile -- HTH, Bob Barrows
From: abruton on 31 Oct 2009 00:52 Hi Bob Thank you for the info. I managed to get it working late last night using the following code... If firstname <> users_firstname Then Set objRoot = xmlObj.documentElement Set objField = objRoot.selectSingleNode("firstname") objField.Text = users_firstname xmlObj.save(cDir & cFile) End If Best regards Andre F Bruton On Oct 30, 5:36 pm, "Bob Barrows" <reb01...(a)NOyahoo.SPAMcom> wrote: > abruton wrote: > > Hi > > > I am trying to update a child node of an XML file IE Changing the > > value. > > > The XML file looks like this: > > > <user> > > <firstname>Andre</firstname> > > <lastname>Bruton</lastname> > > </user> > > > Here is my Classic asp code: > > > users_firstname = "Tristan" 'New code to put in the XML file > > > Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument") > > xmlObj.async = False > > xmlObj.setProperty "ServerHTTPRequest", True > > xmlObj.Load(cURL) > > If xmlObj.parseError.errorCode <> 0 Then > > Response.Write "Error Reaqding File - " & xmlObj.parseError.reason > > & "<p>" > > End If > > > Set xmlList = xmlObj.getElementsByTagName("user") > > For Each xmlItem In xmlList > > For Each xmlItem2 In xmlItem.childNodes > > a = xmlItem2.nodeName > > if a = "firstname" then firstname = xmlItem2.text > > if a = "lastname" then lastname = xmlItem2.text > > Next > > Next > > If you only have a single <user> element, there is no need to do a loop: > just use selectSingleNode: > > set usernode = xmlObj.selectSingleNode("//user") > > > > > > > > > If firstname <> users_firstname Then > > Set nodeBook = xmlObj.selectSingleNode("//firstname") > > nodeBook.setAttribute "firstname", users_firstname > > Response.Write nodeBook.getAttribute("firstname") > > xmlObj.save(cDir & cFile) > > End If > > > Set xmlObj = Nothing > > > The problem is that it adds a new section to the XML file instead of > > updating the value of firstname from Andre to Tristan. The XML looks > > like this: > > > <user> > > <firstname firstname="Andre6">Andre</firstname> > > <lastname>Bruton</lastname> > > </user> > > > What is should look like is: > > > <user> > > <firstname>Tristan</firstname> > > <lastname>Bruton</lastname> > > </user> > > > Any idea how I can fix this? > > Don't use getAttribute or SetAttribute. > This is an attribute: firstname="Andre6" > This is an element: <firstname>Tristan</firstname> > > Untested air code: > set fnamenode = xmlObj.selectSingleNode("//user/firstname") > if fnamenode.nodeValue <> users_firstname then > fnamenode.nodeValue = users_firstname > end if > Response.Write fnamenode.nodeValue > xmlObj.save cDir & cFile > > -- > HTH, > Bob Barrows
|
Pages: 1 Prev: avoiding error? Next: sendmail finnishing |