slimbx
asked on
DOM and insertBefore() function
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_EL EMENT, "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.in sertBefore objNewNode, XML_Doc.documentElement.fi rstChild.f irstChild
'XML_Doc.Validate
'XML_Doc.save App.path & "\alltypeentries.xml"
If XML_Doc.parseError.errorCo de <> 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
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_EL
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.in
'XML_Doc.Validate
'XML_Doc.save App.path & "\alltypeentries.xml"
If XML_Doc.parseError.errorCo
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
b1xml2, patience, I wanted to try it out first :)
Everything worked fine, thank you very much.
Everything worked fine, thank you very much.
ASKER
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
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
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!
ASKER
I think the 'Adding and Removing nodes from XML file ' post will work out for me.
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("
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