Solved

DOM and insertBefore() function

Posted on 2002-03-21
6
219 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
call javascript function from within xslt 4 26
c#, split 12 78
Convert Oracle data into XML document 2 39
XSLT Help 12 21
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

19 Experts available now in Live!

Get 1:1 Help Now