Option Strict On
Option Explicit On
Namespace XML
''' <summary>
''' Represents an entire XML document.
''' </summary>
Class XMLDoc
Inherits XMLToken
''' <summary>
''' Creates a blank XML document.
''' </summary>
Sub New()
MyBase.New("XML", "")
End Sub
''' <summary>
''' Creates and automatically parses an XML document from an existing string.
''' </summary>
''' <param name="XMLString">Existing XML String</param>
Sub New(XMLString As String)
MyBase.New("XML", XMLString)
End Sub
''' <summary>
''' Writes this element as a string.
''' </summary>
Public Overrides Function ToString() As String
Dim TempVal As String = ""
For i As Int32 = 0 To Tokens.Count - 1 Step 1
TempVal &= Tokens(i).ToString & vbNewLine
Next
Return TempVal
End Function
End Class
''' <summary>
''' Represents an XML Token with 4 properties: Name, Attributes, Value, and Child Tokens. Note: A token cannot have both a value and child tokens property.
''' </summary>
Class XMLToken
''' <summary>
''' The name (tag) of the token.
''' </summary>
Property Name As String
''' <summary>
''' The attributes associated with the element, may be blank.
''' </summary>
Property Attributes As String
''' <summary>
''' The value of the token.
''' </summary>
Property Value As String
''' <summary>
''' A list of child tokens.
''' </summary>
Property Tokens As List(Of XMLToken)
''' <summary>
''' Programatically creates a new XMLToken.
''' </summary>
''' <param name="Name_New">Name (tag) Property</param>
''' <param name="Value_New">Value Property (Can be written in XML, automatically parsed)</param>
Sub New(Name_New As String, Value_New As String)
Name = Name_New
Attributes = ""
Value = Value_New
Tokens = New List(Of XMLToken)
Search(Value.Trim)
End Sub
''' <summary>
''' Manually creates a new XMLToken.
''' </summary>
''' <param name="Name_New">Name (tag) Property</param>
''' <param name="Children">The child tokens as a list.</param>
Sub New(Name_New As String, Children As List(Of XMLToken))
Name = Name_New
Attributes = ""
Value = ""
Tokens = New List(Of XMLToken)(Children)
End Sub
''' <summary>
''' Programatically creates a new XMLToken.
''' </summary>
''' <param name="Name_New">Name (tag) Property</param>
''' <param name="Attributes_New">Attributes Property (written="like this")</param>
''' <param name="Value_New">Value Property (Can be written in XML, automatically parsed)</param>
Sub New(Name_New As String, Attributes_New As String, Value_New As String)
Name = Name_New
Attributes = Attributes_New.Trim
Value = Value_New
Tokens = New List(Of XMLToken)
Search(Value.Trim)
End Sub
''' <summary>
''' Manually creates a new XMLToken.
''' </summary>
''' <param name="Name_New">Name (tag) Property</param>
''' <param name="Attributes_New">Attributes Property (written="like this")</param>
''' <param name="Children">The child tokens as a list.</param>
Sub New(Name_New As String, Attributes_New As String, Children As List(Of XMLToken))
Name = Name_New
Attributes = Attributes_New.Trim
Value = ""
Tokens = New List(Of XMLToken)(Children)
End Sub
''' <summary>
''' A somewhat recursive function that creates new tokens and child tokens.
''' </summary>
''' <param name="TokenString">The string to search for tokens, usually the parent token's value.</param>
Private Sub Search(TokenString As String)
Dim Another As Boolean = False
For i As Int32 = 0 To TokenString.Count - "<_></_>".Length Step 1
If TokenString(i) = "<"c Then
i += 1
Dim NameEnd As Int32 = -1
For j As Int32 = i + 1 To TokenString.Count - "></_>".Length Step 1
If (TokenString(j) = " "c OrElse TokenString(j) = ">"c) AndAlso NameEnd = -1 Then NameEnd = j
If TokenString(j) = ">"c Then
Dim TokenName As String = TokenString.Substring(i, NameEnd - i)
Dim TokenAttr As String = TokenString.Substring(NameEnd, j - NameEnd)
Dim TokenEnd As String = $"</{TokenName}>"
j += 1
For k As Int32 = j To TokenString.Length - TokenEnd.Length
If TokenString.Substring(k, TokenEnd.Length) = TokenEnd Then
Dim TokenValue As String = TokenString.Substring(j, k - j)
Tokens.Add(New XMLToken(TokenName, TokenAttr, TokenValue)) ' SUCCESS!
i = k + TokenEnd.Length
If i >= TokenString.Count Then Exit Sub
Another = True
Exit For
End If
Next
If Another Then Exit For
End If
Next
Another = False
End If
Next
End Sub
''' <summary>
''' Retrieves all the child tokens with a specific name.
''' </summary>
''' <param name="TokenName">The name of the token.</param>
Function GetToken(TokenName As String) As List(Of XMLToken)
Dim L As New List(Of XMLToken)
For i As Int32 = 0 To Tokens.Count - 1 Step 1
If Tokens(i).Name = TokenName Then L.Add(Tokens(i))
Next
Return L
End Function
''' <summary>
''' Writes this element as a string.
''' </summary>
Public Overrides Function ToString() As String
If Tokens.Count > 0 Then
Dim TempVal As String = vbNewLine
For i As Int32 = 0 To Tokens.Count - 1 Step 1
TempVal &= Tokens(i).ToString & vbNewLine
Next
Return $"<{Name}{If(Attributes.Length > 0, " ", "")}{Attributes}>{TempVal}</{Name}>"
End If
Return $"<{Name}{If(Attributes.Length > 0, " ", "")}{Attributes}>{Value}</{Name}>"
End Function
End Class
End Namespace