Results 1 to 12 of 12

Thread: [2008] Parsing XML

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    4

    [2008] Parsing XML

    Currently developing monitoring program for Servers, now I have a vb script which runs periodically, and exports its findings to an XML file, I'm struggling to figure out a way to parse the XML file. The XML file is Laid out as follows.

    Code:
    xml version="1.0" encoding="UTF-8"?>
    <Site>
    <SiteID>SID0001</SiteID>
    <SiteName>SITENAME</SiteName>
    <CheckDate>13/01/2009</CheckDate>
    <Server>
    <ServerName>SERVERNAME</ServerName>
    <SerialNumber>7M0W24J</SerialNumber>
    <LastBootTime>19/12/2008 12:42:31</LastBootTime>
    <Drives>
    <Drive>
    <DriveLetter>C:</DriveLetter>
    <DriveCapacity>25603MB</DriveCapacity>
    <DriveRemaining>10.97%</DriveRemaining>
    </Drive>
    <Drive>
    <DriveLetter>D:</DriveLetter>
    <DriveCapacity>212663MB</DriveCapacity>
    <DriveRemaining>84.19%</DriveRemaining>
    </Drive>
    </Drives>
    <OperatingSystem>
    <OSName>Microsoft(R) Windows(R) Server 2003 for Small Business Server</OSName>
    <ServicePack>2</ServicePack>
    </OperatingSystem>
    <Antivirus>
    <Provider>Symantec End Point</Provider>
    <VirusDefs>20090112.041</VirusDefs>
    </Antivirus>
    <UPS>
    </UPS>
    </Server>
    <Exchange>
    <ExchangeServer>EXCHSERVER</ExchangeServer>
    <MailBoxes>
    <MailBoxDetails>
    <DisplayName>Name1</DisplayName>
    <MailBoxSize>1659690MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>Name2</DisplayName>
    <MailBoxSize>1105938MB</MailBoxSize>
    </MailBoxDetails>
    </MailBoxes>
    </Exchange>
    </Site>
    I'm unsure how to parse this.

    Help Appreciated

    Many Thanks

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Parsing XML

    You can use the XML.XMLDocument class to load in your file, then loop through the nodes you want.

    There will be tons of articles on this on google
    EDIT: See http://www.google.co.uk/search?hl=en...parse+xml+file
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Parsing XML

    You want the SelectNodes() and SelectSingleNode() methods, these will help you extract the values out. You'll need to learn a bit of XPath as well to specify which node you want.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Parsing XML

    They are methods of the XMLDocument class though yeah?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Parsing XML

    Yeah. The XmlDocument class would be called XmlUseless without them.

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Parsing XML

    haha ok I was just checking as I've never tried XML parsing before and I figure it would be quite a useful thing to know how to do so I'll get practicing!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    4

    Re: [2008] Parsing XML

    Cool guys will look further into it.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Parsing XML

    Quote Originally Posted by chris128
    haha ok I was just checking as I've never tried XML parsing before and I figure it would be quite a useful thing to know how to do so I'll get practicing!
    Really? I'm pretty sure I've seen you post about those methods and XPath as well. If you don't know about it, you'll love it. SelectNodes() returns an XmlNodeList and SelectSingleNode returns an XmlNode.

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Parsing XML

    Nope, wasnt me I saw this thread and thought "hmm I've never touched XML through VB... I'll see if I can learn something by helping this guy out"

    I've got my test server spec file parsing nicely now using this:
    vb Code:
    1. Dim xmldoc As New xmldocument
    2. xmldoc.Load("C:\test.xml")
    3.  
    4. Dim Nodes As XmlNodeList
    5. Nodes = xmldoc.SelectNodes("/Server/Specs")
    6.  
    7. For Each node As XmlNode In Nodes
    8.      MessageBox.Show(node("HardDrives").InnerText)
    9. Next

    Oh and I found out the hard way that you cant have spaces in xml node names
    Last edited by chris128; Jan 15th, 2009 at 11:46 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Parsing XML

    It's also good that you chose to use the single slash notation rather then double slash. The double slash notation is expensive and can also give incorrect results if there are nested nodes of the same name (which is common).

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Parsing XML

    I see, thanks for the tip
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    4

    Re: [2008] Parsing XML

    Cheers guys sort of getting there, the code chris provided is ace, however I'm struggling when it comes to multiple sites in one XML file as

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Site>
    <SiteID>FUS0003</SiteID>
    <SiteName>ANIND</SiteName>
    <CheckDate>18/01/2009</CheckDate>
    <Server>
    <ServerName>SERVER1</ServerName>
    <SerialNumber>93VDK3J</SerialNumber>
    <LastBootTime>11/11/2008 19:02:58</LastBootTime>
    <Drives>
    <Drive>
    <DriveLetter>C:</DriveLetter>
    <DriveCapacity>25007MB</DriveCapacity>
    <DriveRemaining>39.09%</DriveRemaining>
    </Drive>
    <Drive>
    <DriveLetter>D:</DriveLetter>
    <DriveCapacity>253768MB</DriveCapacity>
    <DriveRemaining>69.40%</DriveRemaining>
    </Drive>
    </Drives>
    <OperatingSystem>
    <OSName>Microsoft(R) Windows(R) Server 2003, Standard Edition</OSName>
    <ServicePack>2</ServicePack>
    </OperatingSystem>
    <Antivirus>
    <Provider>McAffee</Provider>
    <VirusDefs>2009/01/07</VirusDefs>
    </Antivirus>
    <UPS>
    <Type>Internal Battery</Type>
    <Status>OK</Status>
    </UPS>
    </Server>
    <Server>
    <ServerName>SERVER2</ServerName>
    <SerialNumber>7RPXL1J</SerialNumber>
    <LastBootTime>15/12/2008 03:07:12</LastBootTime>
    <Drives>
    <Drive>
    <DriveLetter>C:</DriveLetter>
    <DriveCapacity>31996MB</DriveCapacity>
    <DriveRemaining>50.77%</DriveRemaining>
    </Drive>
    <Drive>
    <DriveLetter>D:</DriveLetter>
    <DriveCapacity>107748MB</DriveCapacity>
    <DriveRemaining>7.68%</DriveRemaining>
    </Drive>
    </Drives>
    <OperatingSystem>
    <OSName>Microsoft(R) Windows(R) Server 2003, Standard Edition</OSName>
    <ServicePack>2</ServicePack>
    </OperatingSystem>
    <Antivirus>
    <Provider>McAffee</Provider>
    <VirusDefs>2009/01/04</VirusDefs>
    </Antivirus>
    <UPS>
    </UPS>
    </Server>
    <Server>
    <ServerName>SERVER3</ServerName>
    <SerialNumber>FBWF42J</SerialNumber>
    <LastBootTime>15/01/2009 03:04:04</LastBootTime>
    <Drives>
    <Drive>
    <DriveLetter>C:</DriveLetter>
    <DriveCapacity>12291MB</DriveCapacity>
    <DriveRemaining>11.16%</DriveRemaining>
    </Drive>
    <Drive>
    <DriveLetter>D:</DriveLetter>
    <DriveCapacity>40962MB</DriveCapacity>
    <DriveRemaining>94.70%</DriveRemaining>
    </Drive>
    <Drive>
    <DriveLetter>E:</DriveLetter>
    <DriveCapacity>139751MB</DriveCapacity>
    <DriveRemaining>53.58%</DriveRemaining>
    </Drive>
    </Drives>
    <OperatingSystem>
    <OSName>Microsoft(R) Windows(R) Server 2003, Standard Edition</OSName>
    <ServicePack>2</ServicePack>
    </OperatingSystem>
    <Antivirus>
    <Provider>McAffee</Provider>
    <VirusDefs>2008/12/28</VirusDefs>
    </Antivirus>
    <UPS>
    </UPS>
    </Server>
    <Server>
    <ServerName>SERVER4</ServerName>
    <SerialNumber>HZQ1D3J</SerialNumber>
    <LastBootTime>01/08/2009 11:34:07</LastBootTime>
    <Drives>
    <Drive>
    <DriveLetter>M:</DriveLetter>
    <DriveCapacity>12291MB</DriveCapacity>
    <DriveRemaining>28.92%</DriveRemaining>
    </Drive>
    <Drive>
    <DriveLetter>N:</DriveLetter>
    <DriveCapacity>126872MB</DriveCapacity>
    <DriveRemaining>95.74%</DriveRemaining>
    </Drive>
    </Drives>
    <OperatingSystem>
    <OSName>Microsoft(R) Windows(R) Server 2003, Standard Edition</OSName>
    <ServicePack>2</ServicePack>
    </OperatingSystem>
    <Antivirus>
    <Provider>McAffee</Provider>
    <VirusDefs>2009/01/17</VirusDefs>
    </Antivirus>
    <UPS>
    </UPS>
    </Server>
    <Exchange>
    <ExchangeServer>SERVER2</ExchangeServer>
    <MailBoxes>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>869866MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1413454MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1244255MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>6111165MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1528535MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>2469953MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1580620MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>2812670MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>2799369MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>846647MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>4528085MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1477715MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1872257MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1898325MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1948138MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>1612821MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>3201482MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>856363MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>2347491MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>2315056MB</MailBoxSize>
    </MailBoxDetails>
    <MailBoxDetails>
    <DisplayName>USER</DisplayName>
    <MailBoxSize>885268MB</MailBoxSize>
    </MailBoxDetails>
    </MailBoxes>
    </Exchange>
    </Site>
    My Code looks like this

    Code:
        Public Sub ImportXMLFile()
            Dim doc As New Xml.XmlDocument
            Dim out As String = ""
            doc.Load(str_Filename)
            Dim Nodes As Xml.XmlNodeList
            Dim Child As Xml.XmlNodeList
    
            'Import Site Data
            Nodes = doc.SelectNodes("/Site")
            For Each node As Xml.XmlNode In Nodes
                out = out + "Site ID         : " + node("SiteID").InnerText & vbCrLf
                out = out + "Site Name       : " + node("SiteName").InnerText & vbCrLf
                out = out + "Check Date      : " + node("CheckDate").InnerText & vbCrLf
                MessageBox.Show(out)
            Next
            'Server Data
            out = ""
            Nodes = doc.SelectNodes("/Site/Server")
            For Each node As Xml.XmlNode In Nodes
                out = out + "Server Name:     " + node("ServerName").InnerText & vbCrLf
                out = out + "Serial Number:    " + node("SerialNumber").InnerText & vbCrLf
                out = out + "Last Boot Time:   " + node("LastBootTime").InnerText & vbCrLf
                MessageBox.Show(out)
                'Drive Information
                Child = doc.SelectNodes("/Site/Server/Drives/Drive")
                For Each noded As Xml.XmlNode In Child
                    out = ""
                    out = out + "Drive Letter:     " + noded("DriveLetter").InnerText & vbCrLf
                    out = out + "Drive Capacity :  " + noded("DriveCapacity").InnerText & vbCrLf
                    out = out + "Drive Remaining : " + noded("DriveRemaining").InnerText & vbCrLf
                    MessageBox.Show(out)
                Next
    
                'OS Information
                Child = doc.SelectNodes("/Site/Server/OperatingSystem")
                For Each noded As Xml.XmlNode In Child
                    out = ""
                    out = out + "OSName :        " + noded("OSName").InnerText & vbCrLf
                    out = out + "Service Pack :  " + noded("ServicePack").InnerText & vbCrLf
                    MessageBox.Show(out)
                Next
    
                'Antivirus Information
                Child = doc.SelectNodes("/Site/Server/Antivirus")
                For Each noded As Xml.XmlNode In Child
                    out = ""
                    out = out + "Provider     :  " + noded("Provider").InnerText & vbCrLf
                    out = out + "Virus Defs   :  " + noded("VirusDefs").InnerText & vbCrLf
                    MessageBox.Show(out)
                Next
    
    
            Next
            'Mailboxes
            Child = doc.SelectNodes("/Site/Exchange/MailBoxes/MailBoxDetails")
            For Each noded As Xml.XmlNode In Child
                out = ""
                out = out + "Mail Box Name :  " + noded("DisplayName").InnerText & vbCrLf
                out = out + "Size          :  " + noded("MailBoxSize").InnerText & vbCrLf
                MessageBox.Show(out)
            Next
    
        End Sub
    But this loops through every drive in the file for each server, any pointers?

    Thanks in advance again.

    Phil

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