Solved

DOM and insertBefore() function

Posted on 2002-03-21
6
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

635 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