Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search and delete 1

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
hi, i got a simple xml as shown below. what's the proper way to search and delete an employee from that xml. i'd like to delete by OID. i tries matches but doesn't bring me anywhere.

help's appreciated

<EMPLOYEES>
<employee>
<id>4</id>
<givenname>hugh</givenname>
<name>spencer</name>
</employee>
<employee>
<id......
</employee>
<EMPLOYEES>
 
Try this in VB6 code:
Code:
Dim oDom As MSXML2.DomDocument40
Dim oNode As MSXML2.IXMLDOMNode
Dim oEmpNode As MSXML2.IXMLDOMNode

' Load XML into oDom

' Remove employee with lIDValue id
Set oNode = oDom.selectSingleNode(&quot;EMPLOYEES/employee/id[text()=&quot; & lIDValue & &quot;]&quot;)
If Not oNode Is Nothing Then
   Set oEmpNode = oNode.ParentNode
   oDom.removeChild(oEmpNode)
End If
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
I am trying to do something similar, but I don't understand the xpath syntax. My xml doc looks like this:

<CDSListBox>
<Item>Remi</Item>
<Item>Kori</Item>
<Item>Tami</Item>
<Item>Arthur</Item>
<Item>Guinevere</Item>
<Item>Gertie</Item>
<Item>testing 1</Item>
<Item>testing2</Item>
<Item>Tami</Item>
<Item>testing3</Item>
</CDSListBox>

My code to delete an item is :

Public Sub DeleteItem(strPath As String, strFileName As String, strDeleteVal As String)
Dim i As Integer
Dim ItemNode As MSXML2.IXMLDOMNode
Dim ItemNodes As MSXML2.IXMLDOMNodeList
Dim ItemVals As MSXML2.IXMLDOMNodeList
Dim mXDoc As New MSXML2.DOMDocument30

On Error GoTo ErrHandler

mXDoc.Load strPath & "\" & strFileName

Set ItemNode = mXDoc.selectSingleNode("CDSListBox/Item[text()=" & strDeleteVal & "]")
Stop
'delete ItemNode Here
end sub

ItemNode continues to be nothing. I'm pretty sure the problem is in the xpath syntax.

Any Ideas ?

thanks,
MFZ
 
Try adding quotes:
"CDSListBox/Item[text()='" & strDeleteVal & "']
 
Jel,

That seems to work but now I want to delete the node I selected. So I get the parent and use removechild. That is not working as well. I don't want to delete all nodes that are of the type Item, just the one I selected.

I've been reviewing the docs on msdn but it's just not making alot of sence. Is there another site that has better docs to learn from ?

Below is the revised code. What do you think ?

Public Sub DeleteItem(strPath As String, strFileName As String, strDeleteVal As String)
Dim i As Integer
Dim ItemNode As MSXML2.IXMLDOMNode
Dim ItemParent As MSXML2.IXMLDOMNode
Dim ItemNodes As MSXML2.IXMLDOMNodeList
Dim mXDoc As New MSXML2.DOMDocument30

On Error GoTo ErrHandler

mXDoc.Load strPath & "\" & strFileName
mXDoc.setProperty "SelectionLanguage", "XPath"
Set ItemNode = mXDoc.selectSingleNode("CDSListBox/Item[text()=" & "'" & strDeleteVal & "'" & "]")

'Get the parent and delete the node just selected
If Not ItemNode Is Nothing Then
Set ItemParent = ItemNode.parentNode
ItemParent.removeChild (ItemNode)
End If




Stop

ErrHandler:

End Sub


Thanks,

MFZ
 
That's just a small VB-syntax thing:
ReturnValue = FunctionName(Argument)
or:
FunctionName Argument

In this case:
set ItemNode = ItemParent.removeChild(ItemNode)
or:
ItemParent.removeChild ItemNode

Don't forget to save the result (you left that out in your function)
 
Thanks that worked. I don't know why I couldent have used FunctionName Argument , but it wouldn't work . I actually do nothing with the ReturnValue.

Thanks,

MFZ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top