XML

From HashVB
Jump to: navigation, search
float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.
 This article is currently work in progress. Please come back later.

Writing XML

80      Set XMLDoc = New DOMDocument
90      Set XMLRoot = XMLDoc.appendChild(XMLDoc.createElement("evidence"))
100     Set XMLItem = XMLRoot.appendChild(XMLDoc.createElement("information"))
 
        'Enumerate the events
110     For EventIndex = 0 To UBound(EventItems)
120       EventItem = EventItems(EventIndex)
 
          'Store the event information in the XML file
130       Set XMLItem = XMLRoot.appendChild(XMLDoc.createElement("event"))
140       XMLItem.setAttribute "id", CStr(EventIndex + 1)
 
          'Its name
150       Set XMLItem2 = XMLItem.appendChild(XMLDoc.createElement("feed"))
160       XMLItem2.setAttribute "id", EventItem.FeedID
170       XMLItem2.setAttribute "name", FeedItems(EventItem.FeedID).Name
 
          'And the time
180       XMLItem.appendChild(XMLDoc.createElement("start")).Text = FormatDateTime(EventItem.Date, vbShortDate) & " " & FormatDateTime(EventItem.Start, vbLongTime)
190       XMLItem.appendChild(XMLDoc.createElement("end")).Text = FormatDateTime(EventItem.Date, vbShortDate) & " " & FormatDateTime(EventItem.End, vbLongTime)
 
380     Next
 
        'Save out the XML
390     XMLDoc.Save Path & "Events.xml"
 

Reading XML

Dim XMLDoc As DOMDocument
Dim XMLRoot As IXMLDOMElement
Dim XMLEvent As IXMLDOMElement
Dim XMLItem As IXMLDOMElement
Dim EventInfo As EventInfo
Dim ListItem As ListItem
  
  'Load the event information
  Set XMLDoc = New DOMDocument
  XMLDoc.Load Path & "Events.xml"
  Set XMLRoot = XMLDoc.documentElement
  
  If XMLRoot Is Nothing Then
    MsgBox "The XML file is missing.", vbExclamation
    Unload Me
    Exit Sub
  End If
  
  Set Events = New Collection
  
  For Each XMLEvent In XMLRoot.getElementsByTagName("event")
    Set EventInfo = New EventInfo
    
    'Get the feed information
    Set XMLItem = XMLEvent.selectSingleNode("feed")
    EventInfo.FeedID = XMLItem.getAttribute("id")
    EventInfo.FeedName = XMLItem.getAttribute("name")
    
    'times
    EventInfo.StartTime = CDate(XMLEvent.selectSingleNode("start").Text)
    EventInfo.EndTime = CDate(XMLEvent.selectSingleNode("end").Text)
    
    'And path
    EventInfo.Path = Path & "Event " & CStr(XMLEvent.getAttribute("id")) & "\"
    
    'Add it to the list
    Events.Add EventInfo
  Next