Cannot display attribute values in XML

Just starting out? Need help? Post your questions and find answers here.
User avatar
Frontier
User
User
Posts: 74
Joined: Thu Dec 22, 2005 2:43 pm
Location: Chios, Greece
Contact:

Cannot display attribute values in XML

Post by Frontier »

Hello,

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>
The file is encoded in Windows-1253 character set.
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
I know that I must be doing something wrong but this is all new for me (the XML stuff), so please be gentle :)
Thank you very much in advance for your help.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Cannot display attribute values in XML

Post by srod »

That XML doesn't have any attributes other than the encoding which is not part of the document content. Works fine here if I add some attributes to the main node or any of the child nodes.
Are you sure it is the attributes you are after and not the node text? Use GetXMLNodeText() to retrieve the node text.
I may look like a mule, but I'm not a complete ass.
User avatar
Frontier
User
User
Posts: 74
Joined: Thu Dec 22, 2005 2:43 pm
Location: Chios, Greece
Contact:

Re: Cannot display attribute values in XML

Post by Frontier »

You are right srod, it was the text I needed to grab, not the attribute :)
Thank you very much for your help.
Post Reply