This is Interesting: Free Magazines for Graphics designers and webmasters
Home > Archive > Microsoft XML > February 2005 > Traversing an XML Document
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Traversing an XML Document
|
|
| Steve 2005-02-21, 11:18 pm |
| I am new to XML and I have a somewhat complex document that will be coming in
from another agency that I need to pull a number of data elements from and
load into a database. The document will have information in it from multiple
people and then I have to pull data related to the person I have selected.
The data will be at various levels of the tree of the document for that
person. The problem I am having is that I have been able to read the
document and get to the person I want but have not been able to get to the
child information for that person. When I try to move down to the child
level I either get the child information on the first person, or a get
nothing at all. Here is the sample XML document that I am playing with.
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2005 U (http://www.xmlspy.com)-->
<!--NOTE: This file shows examples of data, not actual data nor data
resembling real data.-->
<CriminalFiling>
<Case>
<CaseParticipants>
<CaseDefendantParty.Person id="CaseDefendantParty.Person001">
<PersonName>
<PersonGivenName>Sally</PersonGivenName>
<PersonMiddleName>Louise</PersonMiddleName>
<PersonSurName>Lambert</PersonSurName>
</PersonName>
</CaseDefendantParty.Person>
<CaseDefendantParty.Person id="CaseDefendantParty.Person002">
<PersonName>
<PersonGivenName>MaryAlice</PersonGivenName>
<PersonSurName>Rizzo</PersonSurName>
</PersonName>
</CaseDefendantParty.Person>
</CaseParticipants>
</Case>
</CriminalFiling>
I am using VB and have loaded the Microsoft XML SDK and have added the XML
4.0 reference to my project. I have tried many things to move down to the
child level and to then get various child elements. Below is an example that
I have tried to no avail. I will need to move down to the child level and
then walk the nodes collecting various data elements and then will need to go
to grandchild of the original element to extract some data there. Any
suggestions will be appreciated.
Private Sub Form_Load()
' Output string:
Dim strout As String
strout = ""
' Load an XML document into a DOM instance.
Dim oXMLDom As New MsXML2.DOMDocument40
Dim nodeid As IXMLDOMAttribute
oXMLDom.async = False
oXMLDom.validateOnParse = False
oXMLDom.resolveExternals = False
oXMLDom.preserveWhiteSpace = True
personnumber = InputBox("Please enter the person number you want to
select: ", "Person Number")
' MsgBox "Person number is - " + personnumber
If oXMLDom.Load("C:\ICJIS\File A Case\FACSample.xml") = False Then
MsgBox "Failed to load XML data from file."
Exit Sub
End If
'______________________ Find the defendant we want to extract
Dim oNodes As IXMLDOMNodeList
Set oNodes =
oXMLDom.selectNodes("CriminalFiling/Case/CaseParticipants/CaseDefendantParty.Person[@id='CaseDefendantParty.Person002']")
Set oNode = oNodes.nextNode
If Not (oNode Is Nothing) Then
If oNode.nodeName = "CaseDefendantParty.Person" Then
MsgBox ("Person rec, Value = " + oNode.nodeTypedValue + "
**Person is - " + oNode.xml)
Else
MsgBox ("not CaseDefendantParty.Person - " & oNode.xml)
End If
End If
Set oNodes = oXMLDom.selectNodes("PersonName/PersonSurName")
Set oNode = oNodes.nextNode
If Not (oNode Is Nothing) Then
MsgBox "We moved down a level to - " & oNode.xml
Else
MsgBox "We did not find anything when we tried to get the child"
End If
End Sub
| |
| gadrin7@aol.com 2005-02-22, 6:42 pm |
| ; This is a Winbatch script which is similar to VBScript
; lines that start with a ";" are comments...
; I think you want to select single nodes (.selectSingleNode method)
instead of
; using a collection and looping thru them using .nextNode
; first I placed the XML you provided into a file...
xfile = "C:\Jay\xml\CFE.xml"
; open an MSXML 4.0 DOM document...
xmlDoc = ObjectOpen("Msxml2.DOMDocument.4.0")
xmlDoc.async = @False
xmlDoc.load(xfile)
;message("Debug", xmlDoc.xml) ;<-- check to see if it loaded...
; find the person...
PersonNode =
xmlDoc.selectSingleNode("CriminalFiling/Case/CaseParticipants/CaseDefendantParty.Person[@id='CaseDefendantParty.Person002']")
message("Debug", PersonNode.xml) ;<-- view the XML for this node...
; now get the person's info...
PersonInfo = PersonNode.selectSingleNode("./PersonName") ;<-- use the
dot (.) operator to specify the query begins on the Person Node...
message("Debug", PersonInfo.xml) ;<-- view the XML for the PersonName
node...
; loop thru the nodes and view each one's tagname and text...
for x = 0 to PersonInfo.childNodes.length-1
InfoNode = PersonInfo.childNodes.item(x)
message(InfoNode.tagname, InfoNode.text)
next
exit
| |
|
| Thanks for the posting. I tried it and still did not get any child nodes
selected. I had change the code to use select a single node and then changed
it to use the ./PersonName to get the child via the selectSingleNode but
ended up with the node being equal to nothing. Any other thoughts?
Thanks....
- Steve
"gadrin7@aol.com" wrote:
> ; This is a Winbatch script which is similar to VBScript
> ; lines that start with a ";" are comments...
>
> ; I think you want to select single nodes (.selectSingleNode method)
> instead of
> ; using a collection and looping thru them using .nextNode
>
> ; first I placed the XML you provided into a file...
>
> xfile = "C:\Jay\xml\CFE.xml"
>
> ; open an MSXML 4.0 DOM document...
> xmlDoc = ObjectOpen("Msxml2.DOMDocument.4.0")
> xmlDoc.async = @False
> xmlDoc.load(xfile)
>
> ;message("Debug", xmlDoc.xml) ;<-- check to see if it loaded...
>
> ; find the person...
> PersonNode =
> xmlDoc.selectSingleNode("CriminalFiling/Case/CaseParticipants/CaseDefendantParty.Person[@id='CaseDefendantParty.Person002']")
> message("Debug", PersonNode.xml) ;<-- view the XML for this node...
>
> ; now get the person's info...
> PersonInfo = PersonNode.selectSingleNode("./PersonName") ;<-- use the
> dot (.) operator to specify the query begins on the Person Node...
> message("Debug", PersonInfo.xml) ;<-- view the XML for the PersonName
> node...
>
> ; loop thru the nodes and view each one's tagname and text...
> for x = 0 to PersonInfo.childNodes.length-1
> InfoNode = PersonInfo.childNodes.item(x)
> message(InfoNode.tagname, InfoNode.text)
> next
>
> exit
>
>
| |
|
| Thanks. I went back and looked at your code again and noticed that when I
was trying to go down to get the child I was referring to the document rather
than node that I was trying to get the child of. Now everything works fine.
Thanks to all!!!!
"gadrin7@aol.com" wrote:
> ; This is a Winbatch script which is similar to VBScript
> ; lines that start with a ";" are comments...
>
> ; I think you want to select single nodes (.selectSingleNode method)
> instead of
> ; using a collection and looping thru them using .nextNode
>
> ; first I placed the XML you provided into a file...
>
> xfile = "C:\Jay\xml\CFE.xml"
>
> ; open an MSXML 4.0 DOM document...
> xmlDoc = ObjectOpen("Msxml2.DOMDocument.4.0")
> xmlDoc.async = @False
> xmlDoc.load(xfile)
>
> ;message("Debug", xmlDoc.xml) ;<-- check to see if it loaded...
>
> ; find the person...
> PersonNode =
> xmlDoc.selectSingleNode("CriminalFiling/Case/CaseParticipants/CaseDefendantParty.Person[@id='CaseDefendantParty.Person002']")
> message("Debug", PersonNode.xml) ;<-- view the XML for this node...
>
> ; now get the person's info...
> PersonInfo = PersonNode.selectSingleNode("./PersonName") ;<-- use the
> dot (.) operator to specify the query begins on the Person Node...
> message("Debug", PersonInfo.xml) ;<-- view the XML for the PersonName
> node...
>
> ; loop thru the nodes and view each one's tagname and text...
> for x = 0 to PersonInfo.childNodes.length-1
> InfoNode = PersonInfo.childNodes.item(x)
> message(InfoNode.tagname, InfoNode.text)
> next
>
> exit
>
>
|
|
|
| | Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |
|