Solved

Parsing XML stream...not file

Posted on 2004-10-08
9
245 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
Comment Utility
0
 
LVL 36

Author Comment

by:SidFishes
Comment Utility
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
Comment Utility
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
Comment Utility
bah...the dtd referenced in the xml is not right has no reference to rankinfo...hmm
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 36

Author Comment

by:SidFishes
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to dynamically set the form action using jQuery.

743 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

16 Experts available now in Live!

Get 1:1 Help Now