Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How do I insert a node into a xml document?

From: Dwight@-----------.---------.---
To: NULL
Date: 11/3/2004 4:09:00 AM
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.
> > 


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent