Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: How do I insert a node into a xml document? [Thread Next] Re: How do I insert a node into a xml document?To: NULL Date: 11/3/2004 7:18:00 AM Sorted...!
Using the following syntax has sorted it
Call
loDocument.documentElement.selectSingleNode("/Books/Book").insertBefore(loNode, loSubject)
"Dwight" wrote:
> Sorry about this, but this is turning out to be a nightmare for what I deem
> to be a simple task. I have the following XML
>
> <Books>
> <Book>
> <Subject></Subject>
> </Book>
> </Books>
>
> I'm trying to insert a "Author" element above "Subject" but I keep on
> getting the follow error message:
> "Insert position Node must be a Child of the Node to insert under."
>
> My code is as follows
> <<
> Set loSubject = loDocument.selectSingleNode("Books/Book/Subject")
> Set loNode = loDocument.createNode(NODE_ELEMENT, "Author", "NS")
> loNode.Text = "My Author"
>
> Call loDocument.documentElement.insertBefore(loNode, loSubject)
> >>
>
> Any thoughts/views.
>
> TIA
> Dwight
>
>
>
>
> "Dwight" wrote:
>
> > Thanks for this.
> >
> > As soon as I added the "DocumentElement" section in, it worked..!
> >
> > e.g
> > call MyDocument.InsertBefore(oNewNode,oPosition) => does not work
> > call MyDocument.DocumentElement.InsertBefore(oNewNode,oPosition) => works!
> >
> >
> > "Gadrin77" wrote:
> >
> > > I'm using MS XML 4.0, I'm not using VB (but it's very similar) so no
> > > "Set" command is needed...
> > >
> > >
> > > xmlDoc = ObjectOpen("Msxml2.DOMDocument.4.0")
> > > xmlDoc.async = @False
> > > xmlDoc.loadxml("<Book><Author>TJones</Author><Title>My
> > > Book</Title></Book>")
> > >
> > > DaNode = xmlDoc.selectSingleNode("//Book/Title") ;<-- choose the node
> > > AFTER the Author...
> > > newElement = xmlDoc.createElement("Subject")
> > > ; assign the new element a value...
> > > newElement.text = "The Subject"
> > > ; append it to the document...
> > > xmlDoc.documentElement.insertBefore(newElement, DaNode)
> > >
> > > message("Debug", xmlDoc.xml)
> > >
> > > exit
> > >
> > > If <Book> isn't your root you can adjust the Xpath to locate it and
> > > also the
> > > line with .documentElement.
> > >
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
