Uses "Console", "File"
$ProcessedFile = APP_SourcePath+"_Template_.XML"
Function TBMain()
If FILE_Exists($ProcessedFile) = FALSE Then
MsgBox 0, "This example needs the file " + $ProcessedFile + " to work"
Stop
End If
String sXMLBuffer = FILE_Load(APP_SourcePath+"_Template_.XML")
PrintL "Version : ", XML_GetTagParameter(sXMLBuffer, "ThinAIRTemplate", "version")
' -- The error catching is a bit silly, but should work
' If Len(XML_LastError) Then
' PrintL "Error occurred:"
' PrintL XML_LastError
' WaitKey
' Stop
' End If
PrintL "Type : ", XML_GetTagParameter(sXMLBuffer, "ThinAIRTemplate", "type")
PrintL
PrintL "DescriptionShort: ", XML_GetString(sXMLBuffer, "ThinAIRTemplate.Header.DescriptionShort")
PrintL "DescriptionLong : ", XML_GetString(sXMLBuffer, "ThinAIRTemplate.Header.DescriptionLong")
PrintL "Author : ", XML_GetString(sXMLBuffer, "ThinAIRTemplate.Header.Author")
PrintL "Contact : ", XML_GetString(sXMLBuffer, "ThinAIRTemplate.Header.Contact")
PrintL "Version : ", XML_GetString(sXMLBuffer, "ThinAIRTemplate.Header.Version")
PrintL
PrintL "Count : ", XML_GetTagParameter(sXMLBuffer, "ThinAIRTemplate.Files", "count")
PrintL
PrintL "Name : ", XML_GetString(sXMLBuffer, "ThinAIRTemplate.Files.Name")
PrintL "Extension : ", XML_GetString(sXMLBuffer, "ThinAIRTemplate.Files.Extension")
PrintL "Code : ", XML_GetString(sXMLBuffer, "ThinAIRTemplate.Files.Code")
WaitKey
End Function
String XML_LastError
' -- Retrieves string from passed tag
Function XML_GetString(sFile As String, sXPath As String) As String
Return XML_GetString_NthOccurence(sFile, sXPath, 1)
End Function
' -- Retrieves parameter of given tag
Function XML_GetTagParameter(sFile As String, sXPath As String, sParameter As String) As String
' -- By bypassing purge we retrieve the part after tag name
String sBuffer = XML_GetString_NthOccurence(sFile, sXPath, 1, FALSE)
' -- This gives us string of the params, like: type="template" version="1.0"
sBuffer = RTrim$(Extract$(sBuffer, ">"), ">")
' -- This chops each param to single array item
String sParams()
Long nParams = Parse(sBuffer, sParams, " ")
' -- Now we seek for match, and if we do, we return the thing between quotes
Long i
For i = 1 To nParams
If StartsWith(sParams(i)+"=", sParameter) Then
Return Parse$(sParams(i), $DQ, 2)
End If
Next
XML_LastError = "The parameter " + $DQ + sParameter + $DQ + " could not be found for tag " + sXPath
End Function
' -- Retrieves string from nth occurrence of passed tag
Function XML_GetString_NthOccurence(sFile As String, sXPath As String, occurence As Long, Optional purge As Long = TRUE) As String
' -- This makes sure we can process both string buffer or file from disk
String sBuffer
If InStr(sFile, ":") = 2 Then
sBuffer = Acode$(UTF8ToWideChar$(FILE_Load(sFile)))
Else
sBuffer = sFile
End If
' -- Some XMLs have odd tags with > on another line
sBuffer = Replace$(sBuffer, $LF+">", ">")
' -- Here we chop the XML tag path to pieces
String sPathElement()
Long i
Long pathElements = Parse(sXPath, sPathElement, ".")
' -- Here ve linearly chop the original string by tags till we get the result
For i = 1 To pathElements
If i = pathElements-1 Then
If InStr(sBuffer, "<"+sPathElement(i)+">") Then
sBuffer = Grab$(sBuffer, "<"+sPathElement(i)+">", "</"+sPathElement(i)+">", occurence) ' -- For tags without parameters
ElseIf InStr(sBuffer, "<"+sPathElement(i)+" ") Then
sBuffer = Grab$(sBuffer, "<"+sPathElement(i)+" ", "</"+sPathElement(i)+">", occurence) ' -- and for those with
If purge Then
sBuffer = Remain$(sBuffer, ">")
End If
Else
XML_LastError = "Tag " + $DQ + sPathElement(i) + $DQ + " was not found in the path " + sXPath
End If
Else
If InStr(sBuffer, "<"+sPathElement(i)+">") Then
sBuffer = Grab$(sBuffer, "<"+sPathElement(i)+">", "</"+sPathElement(i)+">")
ElseIf InStr(sBuffer, "<"+sPathElement(i)+" ") Then
sBuffer = Grab$(sBuffer, "<"+sPathElement(i)+" ", "</"+sPathElement(i)+">")
If purge Then
sBuffer = Remain$(sBuffer, ">")
End If
Else
XML_LastError = "Tag " + $DQ + sPathElement(i) + $DQ + " was not found in the path " + sXPath
End If
End If
Next
Return sBuffer
End Function
Bookmarks