Solved

DOM and insertBefore() function

Posted on 2002-03-21
6
224 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

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…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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