I have the following XML file that I need to parse (I am new to XML in general).
Using PB 5.70 LTS.
Code: Select all
<?xml version="1.0" encoding="windows-1253"?>
<Title>
<TransSeqNum>0000403852</TransSeqNum>
<GradeID>10</GradeID>
<Product>Αμόλυβδη 95</Product>
<VolumeLt>6.415</VolumeLt>
<MoneyEu>10.00</MoneyEu>
<MoneyEuClean>8.065</MoneyEuClean>
<MoneyEuVat>1.935</MoneyEuVat>
<VatPerc>24.0</VatPerc>
<FpID>2</FpID>
<NozzleID>1</NozzleID>
<FinTime>13/02/2019 06:56:55</FinTime>
<Price>1.559</Price>
<PriceClean>1.257</PriceClean>
<Tank>3</Tank>
<VatNumber></VatNumber>
<LicensePlate></LicensePlate>
</Title>
Using the xm.pb example file, I can get the attribute names but not the values.
I had to supply the #PB_Ascii option to the LoadXML function, in order to use the correct encoding but still attribute values do not appear on the tree.
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Xml
;
; (c) Fantaisie Software
;
; ------------------------------------------------------------
;
#Window = 0
#TreeGadget = 0
#XML = 0
; This procedure fills our TreeGadget, by adding the current node
; and then exploring all childnodes by recursively calling itself.
;
Procedure FillTree(*CurrentNode, CurrentSublevel)
; Ignore anything except normal nodes. See the manual for
; XMLNodeType() for an explanation of the other node types.
;
If XMLNodeType(*CurrentNode) = #PB_XML_Normal
; Add this node to the tree. Add name and attributes
;
Text$ = GetXMLNodeName(*CurrentNode) + " (Attributes: "
If ExamineXMLAttributes(*CurrentNode)
While NextXMLAttribute(*CurrentNode)
Text$ + XMLAttributeName(*CurrentNode) + "=" + Chr(34) + XMLAttributeValue(*CurrentNode) + Chr(34) + " " + GetXMLAttribute(*CurrentNode, XMLAttributeName(*CurrentNode)) + " "
Wend
EndIf
Text$ + ")"
Debug Text$
AddGadgetItem(#TreeGadget, -1, Text$, 0, CurrentSublevel)
; Now get the first child node (if any)
;
*ChildNode = ChildXMLNode(*CurrentNode)
; Loop through all available child nodes and call this procedure again
;
While *ChildNode <> 0
FillTree(*ChildNode, CurrentSublevel + 1)
*ChildNode = NextXMLNode(*ChildNode)
Wend
EndIf
EndProcedure
FileName$ = OpenFileRequester("Choose XML file...", "", "XML files (*.xml)|*.xml|All files (*.*)|*.*", 0)
If FileName$ <> ""
If LoadXML(#XML, FileName$,#PB_Ascii)
; Note:
; The LoadXML() succeed if the file could be read. This does not mean that
; there was no error in the XML though. To check this, XMLStatus() can be
; used.
;
; Display an error message if there was a markup error
;
If XMLStatus(#XML) <> #PB_XML_Success
Message$ = "Error in the XML file:" + Chr(13)
Message$ + "Message: " + XMLError(#XML) + Chr(13)
Message$ + "Line: " + Str(XMLErrorLine(#XML)) + " Character: " + Str(XMLErrorPosition(#XML))
MessageRequester("Error", Message$)
EndIf
; Note:
; Even if there was an error in the XML, all nodes before the error position
; are still accessible, so open the window and show the tree anyway.
;
If OpenWindow(#Window, 0, 0, 500, 500, "XML Example", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
TreeGadget(#TreeGadget, 10, 10, 480, 480)
; Get the main XML node, and call the FillTree() procedure with it
;
*MainNode = MainXMLNode(#XML)
If *MainNode
FillTree(*MainNode, 0)
EndIf
; Expand all nodes for a nicer view
;
For i = 0 To CountGadgetItems(#TreeGadget) - 1
SetGadgetItemState(#TreeGadget, i, #PB_Tree_Expanded)
Next i
; Wait for the window close event.
;
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
Else
MessageRequester("Error", "The file cannot be opened.")
EndIf
EndIf
Thank you very much in advance for your help.