Solved

DOM and insertBefore() function

Posted on 2002-03-21
6
223 Views
Last Modified: 2009-07-29
Hey all, I have a quick one. I am looking for the least amount of code here.

I am using VB, trying to add a node into an xml file. The trick is, I want to add a node named <library>
under the <libraries>. Here is my xml file and vb code thus far:

<ENTRIES>
<LIBRARIES>
<LIBRARY NAME="Lib1" REFERENCE="REF.INI"/>
</LIBRARIES>
<PROJECT>
<PROJECT NAME="Prj1" REFERENCE="REF.INI"/>
</PROJECT>
</ENTRIES>

Here is my vb code
'add the new library entry into alltypeentries.xml entry file
Set objNewNode = XML_Doc.createNode(NODE_ELEMENT, "LIBRARY", vbNullString)
objNewNode.setAttribute "NAME", currentSourceName
If Right(currentSourcePath, 1) = "\" Then
determinedPath = currentSourcePath & currentSourceName & "Settings.ini"
Else
determinedPath = currentSourcePath & "\" & currentSourceName & "Settings.ini"
End If
                            objNewNode.setAttribute "REFERENCE", determinedPath
                            XML_Doc.documentElement.insertBefore objNewNode, XML_Doc.documentElement.firstChild.firstChild
'XML_Doc.Validate
'XML_Doc.save App.path & "\alltypeentries.xml"
                           
If XML_Doc.parseError.errorCode <> 0 Then
MsgBox XML_Doc.parseError.reason & vbCrLf & _
XML_Doc.parseError.Line & vbCrLf & _
XML_Doc.parseError.srcText
Else
MsgBox XML_Doc.xml
End If
                           
'kills/nulls all object variables
'Set newNodeCreated = Nothing
Set objNewNode = Nothing


XML_Doc is my DOMDocument object passed into this function
as New MSXML2.DOMDocument

Any thoughts anyone? I need this asap. Thanks so much in advance.

slimbx                          
0
Comment
Question by:slimbx
  • 3
  • 3
6 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 6886348
Set objNewNode = XML_Doc.createElement("library")
objNewNode.setAttribute "NAME", currentSourceName
If Right(currentSourcePath, 1) = "\" Then
determinedPath = currentSourcePath & currentSourceName & "Settings.ini"
Else
determinedPath = currentSourcePath & "\" & currentSourceName & "Settings.ini"
End If
objNewNode.setAttribute "REFERENCE", determinedPath
'find parent node
Set oParentNode = XML_Doc.selectSingleNode("//LIBRARIES")
If Not oParentNode Is Nothing Then
 'use the appendChild method
 oParentNode.appendChild objNewNode
Else
 'raise error here since parent node
 'could not be found

End If


0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 50 total points
ID: 6888109
feedback pls...since you flagged this as urgent
0
 

Author Comment

by:slimbx
ID: 6888773
b1xml2, patience, I wanted to try it out first :)

Everything worked fine, thank you very much.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:slimbx
ID: 6899474
b1xml2,

The oddest thing happened. When I used the code that you provided, it worked, with the xml file structure looking like:

<parent>
 <child/>
 <child/>
 <child/>
 <child/>
</parent>

But with one of my xml files which looks like this

<parent>
 <child>
   <child></child>
   <child></child>
 </child>
 <child/>
 <child>
   <child></child>
 </child>
</parent>

The code did not work. What seemes to be happening, is that when I select the single node, using an XPath querystring, it locates the node. But when I try to do an insertBefore and save it, it does not insert the node anywhere. Actually, it does not do anything. I have tested this thing for the past three days, tried to move a node back or front and then insertBefore, nothing.

Do you have any additional suggestions?
I would post a new question, BUT has to do with your answer so I dont think that I should. :)

slimbx
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6927538
no such luck for 50 points, I did not post code for insertBefore(), I showed the appendChild() method. To use the insertBefore() method, you need both the new node and the child node!
0
 

Author Comment

by:slimbx
ID: 6928295
I think the 'Adding and Removing nodes from XML file ' post will work out for me.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question