XML and ASP

V

vunet.us

How can I add a child node to XML node "ItemID" below?
Thanks for assistance.

Dim oDOM : Set oDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOM.LoadXML "<rss version=""2.0""></rss>"
AddElem oDOM.documentElement, "ItemID", "15314"

Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function
 
A

Anthony Jones

How can I add a child node to XML node "ItemID" below?
Thanks for assistance.

Dim oDOM : Set oDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOM.LoadXML "<rss version=""2.0""></rss>"
AddElem oDOM.documentElement, "ItemID", "15314"

Function AddElem(roParent, rsName, rvntValue)
Set AddElem = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElem
If Not IsNull(rvntValue) AddElem.Text = rvntValue
End Function

Dim oItem

Set oItem = AddElem(oDOM.documentElement, "ItemID", "15314")
AddElem oItem, "subID", "AB"

This would create the XML:-

<rss version="2.0">
<ItemID>15314<subID>AB</subID></itemID>
</rss>
 
V

vunet.us

Dim oItem

Set oItem = AddElem(oDOM.documentElement, "ItemID", "15314")
AddElem oItem, "subID", "AB"

This would create the XML:-

<rss version="2.0">
<ItemID>15314<subID>AB</subID></itemID>
</rss>

thank you. but how to append new child to subID?
 
A

Anthony Jones

thank you. but how to append new child to subID?

Dim oItem, oSubItem

Set oItem = AddElem(oDOM.documentElement, "ItemID", "15314")
Set oSubItem = AddElem(oItem, "subID", Null)
AddElem oSubItem, "subsub", "Stuff"

Creates:-

<rss version="2.0">
<ItemID>15314<subID><subsub>Stuff</subsub></subID></itemID>
</rss>
 
V

vunet.us

Dim oItem, oSubItem

Set oItem = AddElem(oDOM.documentElement, "ItemID", "15314")
Set oSubItem = AddElem(oItem, "subID", Null)
AddElem oSubItem, "subsub", "Stuff"

Creates:-

<rss version="2.0">
<ItemID>15314<subID><subsub>Stuff</subsub></subID></itemID>
</rss>

Sorry for asking more, but... how can I add an attribute?
 
A

Anthony Jones

Sorry for asking more, but... how can I add an attribute?

Dim oItem, oSubItem
Set oItem = AddElem(oDOM.documentElement, "ItemID", "15314")
Set oSubItem = AddElem(oItem, "subID", Null)
AddElem oSubItem, "subsub", "Stuff"
oSubItem.setAttrbute("thing", "blah")

generates:-

<rss version="2.0">
<ItemID>15314<subID><subsub thing="blah" >Stuff</subsub></subID></itemID>
</rss>

Try http://www.w3schools.com/

and

http://msdn.microsoft.com/library/en-us/xmlsdk/html/e9da2722-7879-4e48-869c-7f16714e2824.asp
 
V

vunet.us

Dim oItem, oSubItem
Set oItem = AddElem(oDOM.documentElement, "ItemID", "15314")
Set oSubItem = AddElem(oItem, "subID", Null)
AddElem oSubItem, "subsub", "Stuff"
oSubItem.setAttrbute("thing", "blah")

generates:-

<rss version="2.0">
<ItemID>15314<subID><subsub thing="blah" >Stuff</subsub></subID></itemID>
</rss>

Tryhttp://www.w3schools.com/

and

http://msdn.microsoft.com/library/en-us/xmlsdk/html/e9da2722-7879-4e4...

Yes, thanks a lot. I'll try those links. Though now I am trying to
modify function to set attribute and it says:

Cannot use parentheses when calling a Sub
/vadmin/rss_items.asp, line 22
AddElemAttr.setAttribute(rvntAttr,rvntAttr)
-------------------------------------------^

Function AddElemAttr(roParent, rsName, rvntValue, rvntAttr,
rvntAttrValue)
Set AddElemAttr = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElemAttr
If Not IsNull(rvntValue) then
AddElemAttr.Text = rvntValue
End If
If Not IsNull(rvntAttr) and rvntAttr <> "" then
AddElemAttr.setAttribute(rvntAttr,rvntAttr)
'Set attr = roParent.ownerDocument.createAttribute("currency")
'AddElemAttr.setAttribute(attr)
End If
End Function

Set oItem = AddElem(oTitle, "item", Null)
AddElem oItem, "price", rsItem("Price") 'NEED ATTRIBUTE TO INSERT
AddElemAttr oItem, "edgeio:price", rsItem("Price"), "currency", "USD"
 
B

Bob Barrows [MVP]

Yes, thanks a lot. I'll try those links. Though now I am trying to
modify function to set attribute and it says:

Cannot use parentheses when calling a Sub
/vadmin/rss_items.asp, line 22
AddElemAttr.setAttribute(rvntAttr,rvntAttr)
-------------------------------------------^

For once, we have an error message that means exactily what it says.
setAttribute is a method that does not return a value, aks, a "Sub"

See http://blogs.msdn.com/ericlippert/archive/2003/09/15/52996.aspx
 
A

Anthony Jones

Yes, thanks a lot. I'll try those links. Though now I am trying to
modify function to set attribute and it says:

Cannot use parentheses when calling a Sub
/vadmin/rss_items.asp, line 22
AddElemAttr.setAttribute(rvntAttr,rvntAttr)
-------------------------------------------^

Function AddElemAttr(roParent, rsName, rvntValue, rvntAttr,
rvntAttrValue)
Set AddElemAttr = roParent.ownerDocument.createElement(rsName)
roParent.appendChild AddElemAttr
If Not IsNull(rvntValue) then
AddElemAttr.Text = rvntValue
End If
If Not IsNull(rvntAttr) and rvntAttr <> "" then
AddElemAttr.setAttribute(rvntAttr,rvntAttr)
'Set attr = roParent.ownerDocument.createAttribute("currency")
'AddElemAttr.setAttribute(attr)
End If
End Function

Set oItem = AddElem(oTitle, "item", Null)
AddElem oItem, "price", rsItem("Price") 'NEED ATTRIBUTE TO INSERT
AddElemAttr oItem, "edgeio:price", rsItem("Price"), "currency", "USD"

First off my fault my code should've looked like this:-

Dim oItem, oSubItem
Set oItem = AddElem(oDOM.documentElement, "ItemID", "15314")
Set oSubItem = AddElem(oItem, "subID", Null)
AddElem oSubItem, "subsub", "Stuff"
oSubItem.setAttribute "thing", "blah"

and i's output is actually:-

<rss version="2.0">
<ItemID>15314<subID thing="blah"><subsub>Stuff</subsub></subID></itemID>
</rss>


Secondly AddElemAttr?? Honestly just don't do that. A function should do
one thing and do that one thing well.
You are trying to create a function that does two things. What happens when
you want to create an element that has two attributes, are you going to
create yet another function for that?

Take this XML:-

<root>
<item>
<name>Hello</name>
<subItem ID="1">Stuff</subItem>
</item>
<item>
<name>World</name>
<subItem ID="2" other="ruhbarb">Blah</subItem>
</item>
</root>

This can built with this code:-

Dim oItem

Dim oDOM : Set oDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOM.loadXML("<root />")

Set oItem = AddElem(oDOM.documentElement, "item", Null)
AddElem oItem, "name", "Hello"
AddElem(oItem, "subItem", "Stuff").setAttribute("ID", "1")

Set oItem = AddElem(oDOM.documentElement, "item", Null)
AddElem oItem, "name", "Hello"
With AddElem(oItem, "subItem", "Blah")
.setAttribute("ID", "2")
.setAttribute("other", "ruhbarb")
End With
 
V

vunet.us

First off my fault my code should've looked like this:-

Dim oItem, oSubItem
Set oItem = AddElem(oDOM.documentElement, "ItemID", "15314")
Set oSubItem = AddElem(oItem, "subID", Null)
AddElem oSubItem, "subsub", "Stuff"
oSubItem.setAttribute "thing", "blah"

and i's output is actually:-

<rss version="2.0">
<ItemID>15314<subID thing="blah"><subsub>Stuff</subsub></subID></itemID>
</rss>

Secondly AddElemAttr?? Honestly just don't do that. A function should do
one thing and do that one thing well.
You are trying to create a function that does two things. What happens when
you want to create an element that has two attributes, are you going to
create yet another function for that?

Take this XML:-

<root>
<item>
<name>Hello</name>
<subItem ID="1">Stuff</subItem>
</item>
<item>
<name>World</name>
<subItem ID="2" other="ruhbarb">Blah</subItem>
</item>
</root>

This can built with this code:-

Dim oItem

Dim oDOM : Set oDOM = Server.CreateObject("MSXML2.DOMDocument.3.0")
oDOM.loadXML("<root />")

Set oItem = AddElem(oDOM.documentElement, "item", Null)
AddElem oItem, "name", "Hello"
AddElem(oItem, "subItem", "Stuff").setAttribute("ID", "1")

Set oItem = AddElem(oDOM.documentElement, "item", Null)
AddElem oItem, "name", "Hello"
With AddElem(oItem, "subItem", "Blah")
.setAttribute("ID", "2")
.setAttribute("other", "ruhbarb")
End With

but this still generates the same error "Cannot use parentheses when
calling a Sub":

AddElem oItem, "Type", "lskjdhf"
With AddElem(oItem, "price", rsItem("Price"))
.setAttribute("ID", "2")
.setAttribute("other", "ruhbarb")
End With
 
A

Anthony Jones


but this still generates the same error "Cannot use parentheses when
calling a Sub":

AddElem oItem, "Type", "lskjdhf"
With AddElem(oItem, "price", rsItem("Price"))
.setAttribute("ID", "2")
.setAttribute("other", "ruhbarb")
End With

I did it again didn't I (flicking between JScript and VBScript is quite
maddening). Tell me, what do think needs to be done to fix that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top