Results 1 to 5 of 5

Thread: [RESOLVED] Parsing XML

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Resolved [RESOLVED] Parsing XML

    Hello, I'm trying to parse a XML file, but I have never done that before. I've looked at a whole bunch of examples, but I'm already stuck at the beginning.

    I need to have the following parts:
    On the 'file poster' line the filename "mts-mrb.rar" in the subject between """
    From each segment the segment bytes, segment number and the long string after the number (the number of segments are not always 10)

    Then the same with the next part, filename "mts-mrb.r00" on the next 'file poster' line, etc.

    Code:
    <?xml version="1.0" encoding="iso-8859-1" ?>
    <!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">
    <!-- NZB Generated by Binsearch.info -->
    <nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">
    
    <file poster="[email protected] (YencL8 (Chil))" date="1175837221" subject="(Test Rar File)&quot;mts-mrb.rar&quot; yEnc (1/10)">
    <groups><group>alt.binaries.paxer</group></groups>
    <segments>
    <segment bytes="258743" number="1">[email protected]</segment>
    <segment bytes="258871" number="2">[email protected]</segment>
    <segment bytes="258724" number="3">[email protected]</segment>
    <segment bytes="258831" number="4">[email protected]</segment>
    <segment bytes="258642" number="5">[email protected]</segment>
    <segment bytes="258797" number="6">[email protected]</segment>
    <segment bytes="258783" number="7">[email protected]</segment>
    <segment bytes="258687" number="8">[email protected]</segment>
    <segment bytes="258790" number="9">[email protected]</segment>
    <segment bytes="25854" number="10">[email protected]</segment>
    </segments>
    </file>
    
    <file poster="[email protected] (YencL8 (Chil))" date="1175849534" subject="(Test Rar File)&quot;mts-mrb.r00&quot; yEnc (1/10)">
    <groups><group>alt.binaries.paxer</group></groups>
    <segments>
    <segment bytes="256615" number="1">[email protected]</segment>
    <segment bytes="256502" number="2">[email protected]</segment>
    <segment bytes="256237" number="3">[email protected]</segment>
    <segment bytes="256238" number="4">[email protected]</segment>
    <segment bytes="256139" number="5">[email protected]</segment>
    <segment bytes="256558" number="6">[email protected]</segment>
    <segment bytes="256618" number="7">[email protected]</segment>
    <segment bytes="256658" number="8">[email protected]</segment>
    <segment bytes="256713" number="9">[email protected]</segment>
    <segment bytes="25813" number="10">[email protected]</segment>
    </segments>
    </file>
    </nzb>
    I'm getting an "Expected token 'EOF' found 'NAME'" error.
    Can anyone please tell me how to start with this? Thank you.

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim oxmlNodeList As IXMLDOMNodeList
    4.     Dim XMLDoc As MSXML2.DOMDocument40
    5.    
    6.     Set XMLDoc = New DOMDocument40
    7.     XMLDoc.async = False
    8.     XMLDoc.validateOnParse = False
    9.     XMLDoc.preserveWhiteSpace = False
    10.    
    11.     XMLDoc.Load "C:\1175965453.xml"
    12.    
    13.     If XMLDoc.parseError.errorCode = 0 Then
    14.         If XMLDoc.readyState = 4 Then
    15.             DoEvents
    16.         End If
    17.     Else
    18.         Err.Description = XMLDoc.parseError.reason & vbCrLf & _
    19.         "Line: " & XMLDoc.parseError.Line & vbCrLf & _
    20.         "XML: " & XMLDoc.parseError.srcText
    21.         Err.Raise 1006
    22.     End If
    23.    
    24.  
    25.     Set oxmlNodeList = XMLDoc.documentElement.selectNodes("/nzb xmlns")
    26.    
    27.  
    28.     Set XMLDoc = Nothing
    29.     Set oxmlNodeList = Nothing
    30. End Sub
    Last edited by Chris001; Sep 12th, 2010 at 12:40 PM.

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Parsing XML

    You can start with this!!
    Code:
    Dim XMLDoc As DOMDocument30 ' Add reference to "Microsoft XML"
    Dim Computers As IXMLDOMNodeList
    Dim Computer As IXMLDOMNode
    
    
    Set XMLDoc = CreateObject("Microsoft.XMLDOM")
    XMLDoc.async = False
    If Not XMLDoc.Load("C:\1175965453.xml") Then
        MsgBox XMLDoc.parseError.reason
    Else
        Set Computers = XMLDoc.selectNodes("//file")
        For Each Computer In Computers
            MsgBox Computer.Attributes.getNamedItem("subject").nodeTypedValue
        Next Computer

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Parsing XML

    Thank you very much.

    Is there also a way to read every <file poster></file> part separately?
    In your example it reads all "subjects" first, but it is important that all the data in a <file poster></file> part is grabbed first before moving on to the next one.

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Parsing XML

    See if this works
    Code:
    Private Sub Command1_Click()
    Dim objDoc As MSXML2.DOMDocument40
    Dim objNodeList As IXMLDOMNodeList
    Dim objSubNodeList As MSXML2.IXMLDOMNodeList
    Dim objAttr As MSXML2.IXMLDOMAttribute
    Dim objNode As MSXML2.IXMLDOMNode
    Dim objSubNode As MSXML2.IXMLDOMNode
    
        Set objDoc = New MSXML2.DOMDocument
        objDoc.async = False
        objDoc.Load "c:\1175965453.xml"
        
        Set objNodeList = objDoc.selectNodes("//file")
        For Each objNode In objNodeList
            Set objAttr = objNode.Attributes.getNamedItem("subject")
            Debug.Print "Subject = " & objAttr.Text
            
            Set objSubNodeList = objNode.selectNodes("segments/segment")
            For Each objSubNode In objSubNodeList
                Set objAttr = objSubNode.Attributes.getNamedItem("bytes")
                Debug.Print vbTab & objAttr.Name & " = " & objAttr.Text
                
                
                Set objAttr = objSubNode.Attributes.getNamedItem("number")
                Debug.Print vbTab & objAttr.Name & " = " & objAttr.Text
                
                Debug.Print vbTab & objSubNode.Text
            Next objSubNode
        Next objNode
        
        Set objAttr = Nothing
        Set objSubNode = Nothing
        Set objSubNodeList = Nothing
        Set objNode = Nothing
        Set objNodeList = Nothing
        Set objDoc = Nothing
    End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Parsing XML

    That works great. Thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width