Solved

Parsing XML stream...not file

Posted on 2004-10-08
9
247 Views
Last Modified: 2013-11-19
i'm trying to figure out how to parse an xml doc and populate a text box on a form...iv'e setup ITC to query seti@home and return an xml doc...this doc is passed to strXML and then I'm trying to parse it with the following (from topxml)

Dim objDOMDocument As DOMDocument
Dim objNodeList As IXMLDOMNodeList
Set objDOMDocument = New DOMDocument
objDOMDocument.async = False
objDOMDocument.loadXML (strXML)
Set objNodeList = objDOMDocument.getElementsByTagName("*")
Set objNodeItems = objNode.getElementsByTagName("name") '<<<<Object required error here
txtName.Text = objNodeItems.Item(0).nodeTypedValue

the xml returned looks like this

<?xml
version="1.0"
encoding="iso-8859-1"
?>

<
DOCTYPE
userstats
SYSTEM
"http://setiathome.ssl.berkeley.edu/xml/userstats.dtd">
<userstats>
<userinfo>
<name>SidFishes</name>

blah blah blah


now this is all new territory for me so i may be completely on the wrong track..pointers in the right direction are appreciated...

0
Comment
Question by:SidFishes
  • 5
  • 2
9 Comments
 
LVL 10

Expert Comment

by:anv
ID: 12265421
0
 
LVL 36

Author Comment

by:SidFishes
ID: 12266475
hmmm....this is what i've got so far i've modified it to not load the file from disk but to use the string returned

problem is that at " For Each objBookNode In objNodes" it skips over this  

If objBookNode.selectNodes("userinfo").length <> 0 Then
         NodeValue = objBookNode.selectSingleNode("userinfo").nodeTypedValue
         Debug.Print NodeValue
      End If

and just moves to the     For Each objBookNode In objNodes statement

so it appears its not finding objBookNode in objNodes

again the xml structure is

<userstats>
    <userinfo>
         <name>Blah </name>
    </userinfo>
</userstats>


Private Sub GetStats_Click()
Dim strXML As String

Inet1.AccessType = icUseDefault
strXML = Inet1.OpenURL("http://setiathome2.ssl.berkeley.edu/fcgi-bin/fcgi?cmd=user_xml&email="sidfishes")

Call LoadXMLFiles(strXML)
Call ReadFile
Me!Status = "-- Ready --"

End Sub
Sub LoadXMLFiles(strXMLName As String)
On Error GoTo ErrorHandler   ' Enable error-handling routine.
    objXMLDOM.async = False
    objXMLDOM.loadXML (strXMLName)
    Exit Sub           ' Exit to avoid handler.
ErrorHandler:            ' Error-handling routine.
    Set objXMLDOM = Nothing
    End
End Sub
Sub ReadFile()
On Error GoTo ErrorHandler   ' Enable error-handling routine.
Dim NodeValue
    Set objNodes = objXMLDOM.selectNodes("userstats")
   For Each objBookNode In objNodes
    If objBookNode.selectNodes("userstats").length <> 0 Then
         NodeValue = objBookNode.selectSingleNode("userstats").nodeTypedValue
         Debug.Print NodeValue
    End If
   
    Next objBookNode
    Set objNodes = objXMLDOM.selectNodes("userstats/userinfo")
    For Each objBookNode In objNodes
      If objBookNode.selectNodes("userinfo").length <> 0 Then
         NodeValue = objBookNode.selectSingleNode("userinfo").nodeTypedValue
         Debug.Print NodeValue
      End If
      If objBookNode.selectNodes("groupinfo").length <> 0 Then
         NodeValue = objBookNode.selectSingleNode("groupinfo").nodeTypedValue
         Debug.Print NodeValue
      End If
      If objBookNode.selectNodes("rankinfo").length <> 0 Then
         NodeValue = objBookNode.selectSingleNode("rankinfo").nodeTypedValue
         Debug.Print NodeValue
      End If
    Next objBookNode
    Set objNodes = objXMLDOM.selectNodes("userstats/userinfo/name")
    For Each objBookNode In objNodes
      If objBookNode.selectNodes("name").length <> 0 Then
         NodeValue = objBookNode.selectSingleNode("name").nodeTypedValue
         Debug.Print NodeValue
      End If
      If objBookNode.selectNodes("numresults").length <> 0 Then
         NodeValue = objBookNode.selectSingleNode("numresults").nodeTypedValue
         Debug.Print NodeValue
      End If
    Next objBookNode
    Exit Sub      ' Exit to avoid handler.
ErrorHandler:       ' Error-handling routine.
   End
End Sub
0
 
LVL 36

Author Comment

by:SidFishes
ID: 12266624
some more info


if i add this bit of code

Dim objDOMDoc As New MSXML2.DOMDocument40
   
    objDOMDoc.async = False
    If objDOMDoc.loadXML(strXML) Then
        MsgBox objDOMDoc.xml
    Else
        Debug.Print "Error: " & objDOMDoc.parseError.reason
    End If
   
    Set objDOMDoc = Nothing

I get
Error: The element 'rankinfo' is used but not declared in the DTD/Schema.

so could this be a problem with the xml document?
0
 
LVL 36

Author Comment

by:SidFishes
ID: 12266901
bah...the dtd referenced in the xml is not right has no reference to rankinfo...hmm
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 36

Author Comment

by:SidFishes
ID: 12268728
after i found the dtd problem it was a little easier to figure out something

this is what i came up with and it works fine...could not get your solution to work but thx for the effort

Private Sub GetStats_Click()
Dim strXML As String
If IsNull(Me!inEmail) Or IsEmpty(inEmail) Or inEmail = "" Then
MsgBox "Please enter a valid email address"
Exit Sub
End If
Inet1.AccessType = icUseDefault
strXML = Inet1.OpenURL("http://setiathome2.ssl.berkeley.edu/fcgi-bin/fcgi?cmd=user_xml&email=" & Me!inEmail & "")
strXML = CStr(strXML)
If InStr(1, strXML, "No user with that name was found") Then
MsgBox strXML
Exit Sub
 End If
If InStr(1, strXML, "error") Then
MsgBox "An error retrieving your stats has occurred. Please try again."
Exit Sub
End If

'because the Seti dtd is incorrect, we need to remove the pointer to it in the xml
'file. If you are modifying this code for use with other sites, you can safely remove
'this next line...unless that DTD is messed up too
strXML = Replace(strXML, "<!DOCTYPE userstats SYSTEM ""http://setiathome.ssl.berkeley.edu/xml/userstats.dtd"">", "")

Dim objDOMDoc As New MSXML2.DOMDocument40
Dim ResultNode As MSXML2.IXMLDOMNode
objDOMDoc.async = False
If objDOMDoc.loadXML(strXML) Then
    Set ResultNode = objDOMDoc.selectSingleNode("/userstats/userinfo/name")
    If ResultNode Is Nothing Then
        Me!xmlName = "n/a"
    Else
       Me!xmlName = ResultNode.Text
    End If
 Set ResultNode = objDOMDoc.selectSingleNode("/userstats/userinfo/numresults")
    If ResultNode Is Nothing Then
        Me!xmlNumResults = "n/a"
    Else
       Me!xmlNumResults = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/userinfo/cputime")
    If ResultNode Is Nothing Then
         Me!xmlCPUTime = "n/a"
    Else
       Me!xmlCPUTime = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/userinfo/avecpu")
    If ResultNode Is Nothing Then
         Me!xmlAveCPU = "n/a"
    Else
       Me!xmlAveCPU = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/userinfo/resultsperday")
    If ResultNode Is Nothing Then
        Me!xmlResultsPerDay = "n/a"
    Else
       Me!xmlResultsPerDay = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/userinfo/lastresulttime")
    If ResultNode Is Nothing Then
        Me!xmllastresulttime = "n/a"
    Else
       Me!xmllastresulttime = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/userinfo/regdate")
    If ResultNode Is Nothing Then
        Me!xmlregdate = "n/a"
    Else
       Me!xmlregdate = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/userinfo/usertime")
    If ResultNode Is Nothing Then
        Me!xmlusertime = "n/a"
    Else
       Me!xmlusertime = ResultNode.Text
    End If
    'end of user info node
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/groupinfo/group")
    If ResultNode Is Nothing Then
        Me!xmlgroup = "n/a"
    Else
       Me!xmlgroup = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/groupinfo/founder")
    If ResultNode Is Nothing Then
            Me!xmlfounder = "n/a"
    Else
       Me!xmlfounder = ResultNode.Text
    End If
    'end of groupinfo node
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/rankinfo/rank")
    If ResultNode Is Nothing Then
        Me!xmlRank = "n/a"
    Else
       Me!xmlRank = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/rankinfo/ranktotalusers")
    If ResultNode Is Nothing Then
        Me!xmlranktotalusers = "n/a"
    Else
       Me!xmlranktotalusers = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/rankinfo/num_samerank")
    If ResultNode Is Nothing Then
         Me!xmlnum_samerank = "n/a"
    Else
       Me!xmlnum_samerank = ResultNode.Text
    End If
Set ResultNode = objDOMDoc.selectSingleNode("/userstats/rankinfo/top_rankpct")
    If ResultNode Is Nothing Then
          Me!xmltop_rankpct = "n/a"
    Else
       Me!xmltop_rankpct = ResultNode.Text
    End If
Else
    MsgBox "Error: " & objDOMDoc.parseError.reason
End If
Me!Status = "-- Ready --"
end Sub
0
 
LVL 10

Expert Comment

by:anv
ID: 12273849
i think the link i gave had some useful information for SidFishes

if SidFishes, doesnot think that ways...and still wants to refund i ve no objection..

0
 
LVL 36

Author Comment

by:SidFishes
ID: 12273894
i'm not worried about the points one way or the other...but the link really didn't help as i wasn't any further ahead after reading it. Couldn't get it to work even after i figured out the problem with the DTD. The solution came in part from an article I read on another site after googling for several hours.

0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12298575
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb script Microsoft Word tables portion of based on bookmark, count and add rows 1 48
Element alignment and word wrapping 9 62
Help me. 3 48
vbModal 12 37
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

895 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

12 Experts available now in Live!

Get 1:1 Help Now