Solved

Parsing XML stream...not file

Posted on 2004-10-08
9
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

623 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