Link to home
Start Free TrialLog in
Avatar of Keith McElroy
Keith McElroy

asked on

vb script / classic asp: change nodeName

Using vb script and classic asp, how do I make this code work:

for each nd in myxml.selectNodes("//somenode")
            nd.nodename = "newnodename"
next
Avatar of Keith McElroy
Keith McElroy

ASKER

for each nd in myxml.selectNodes("//somenode")
            nd.nodename = "newnodename"
next
whats the current problem?
Hi HainKurt!
Thank you for reaching out.
The current problem is that
nd.NodeName = "newnodename"
stops the script
call nd.setAttribute("NodeName","newnodename") does not error but as expected just changes
an attribute in the node.  It is the tag, or nodename that I need to change.
Keith
can you attach an xml
before and after the code - that you want to get...
<runner>
    <somenode attr="anattribute">text</somenode>
   <othernode>text</othernode>
  <othernode>
         <somenode>text</somenode>
</runner>

I am using this to get the functionality working.  
A successful run of my script would look like this

<runner>
    <newnodename attr="anattribute">text</newnodename>
   <othernode>text</othernode>
  <othernode>
         <newnodename>text</newnodename>
</runner>
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, this is the guidance I needed.
Appreciate it!
I still need to figure out how to include the attribute in the new node and figure out what pn. is
but this should get me moving the right direction!
Keith



I was playing with with that sample xml
and deleted some old code/comments but appearently deleted more :)

here is the working sample

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

xmlDoc.Async = "False"
xmlDoc.Load("books.xml")
Set books = xmlDoc.selectNodes ("//bookstore/book")

For Each book in books
   set pn = book.getElementsByTagName("price")(0)
   'price = pn.firstChild.Text
   price = pn.firstChild.nodeValue
   MsgBox (price)

   Dim objCurrNode, objNewNode, objNewText
   Set objNewNode = xmlDoc.createElement("newPrice")
   Set objNewText = xmlDoc.createTextNode(price)
   objNewNode.appendChild(objNewText)
   
   book.appendChild(objNewNode)
   book.removeChild(pn)
Next

xmlDoc.Save("books2.xml")

Open in new window

and this is books2.xml
<bookstore>
   <book category="cooking">
      <title lang="en">Everyday Italian</title>
      <author>Giada De Laurentiis</author>
      <year>2005</year>
      <newPrice>33</newPrice>
   </book>
   <book category="children">
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <newPrice>32.989</newPrice>
   </book>
</bookstore>

Open in new window

add new attributes to the node (add this after Line 13):

objNewNode.setAttribute "modifiedBy","HainKurt"

Open in new window


>>>

   <book category="children">
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <newPrice modifiedBy="HainKurt">32.989</newPrice>
   </book>

Open in new window

thank you, I think I got this.  Thanks to you!
Cheers,
K