Solved

DOM and insertBefore() function

Posted on 2002-03-21
6
220 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

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.

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now