Namespace ActiveBasic Namespace Xml Class ParserException Inherits System.Exception Public Sub ParserException( message As String ) Exception( message ) End Sub End Class Class Parser xmlString As String currentIndex As Long doc As System.Xml.XmlDocument Static Const tokenLPar = &H3C ' < Static Const tokenRPar = &H3E ' > Static Const tokenSlash = &H2F ' / Static Const xmlHeaderStr = "" If xmlString[currentIndex] = tokenRPar Then ' '>' ' ">" で閉じられている currentIndex ++ Do SkipWhiteSpace() If xmlString[currentIndex] = 0 Then Throw New ParserException( xmlElement.LocalName + Ex"の閉じタグが見つからない" ) End If If xmlString[currentIndex] = tokenLPar Then ' '<' ' タグ If StringWordEquals( closeTagStr ) Then ' タグが閉じられた currentIndex += closeTagStr.Length Exit Do End If xmlElement.AppendChild( ParseNode() ) Else ' タグ以外 xmlElement.AppendChild( doc.CreateTextNode( ReadTextArea() ) ) End If Loop Else ' "/>" で閉じられている currentIndex += 2 End If Return xmlElement End Function Function ParseNode() As System.Xml.XmlNode If Not xmlString[currentIndex] = tokenLPar Then ' "<" で開始できないとき Throw New ParserException( Ex"\q<\q で開始できない" ) End If If StringWordEquals( xmlHeaderStr ) Then ' XMLヘッダ Return ParseDeclaration() ElseIf StringWordEquals( commentHeader ) Then ' コメントヘッダ Return ParseComment() ElseIf StringWordEquals( cdataHeader ) Then ' CDATAヘッダ Return ParseCData() ElseIf StringWordEquals( dtdHeader ) Then ' DTDヘッダ Return ParseDtd() Else ' その他のタグ Return ParseElement() End If End Function Public Sub Parser( xmlString As String, doc As System.Xml.XmlDocument ) This.xmlString = xmlString This.doc = doc End Sub Sub Parse() This.currentIndex = 0 This.doc.RemoveAll() SkipWhiteSpace() While currentIndex < xmlString.Length This.doc.AppendChild( ParseNode() ) SkipWhiteSpace() Wend End Sub Static Sub Parse( xmlString As String, doc As System.Xml.XmlDocument ) Dim parser = New Parser( xmlString, doc ) parser.Parse() End Sub End Class End Namespace End Namespace