ExtractXMLList behaviour

Just starting out? Need help? Post your questions and find answers here.
User avatar
Plan_B
New User
New User
Posts: 4
Joined: Thu Nov 23, 2017 5:32 pm

ExtractXMLList behaviour

Post by Plan_B »

Hello again

I'm currently banging my head on the table for finding the problem with the ExtractXMLList Function. An example:

Code: Select all

;# Structure
Structure Printer
  Id.l
  Name.s
EndStructure

;# Globals
Global NewList Printer.Printer()
Global XML$ = "<?xml version="+Chr(34)+"1.0"+Chr(34)+"?><Printers><Printer><Id>250</Id><Name>Agfa 901</Name></Printer><Printer><Id>300</Id><Name>Agfa X300</Name></Printer></Printers>"

;# XML
myXML = ParseXML(#PB_Any, XML$)

If myXML = 0
  Debug "Warning: XML could not be parsed."
EndIf

If XMLStatus(myXML) <> #PB_XML_Success
  Debug "Warning: XML could not be processed."
Else
  Debug "Info: XML parsed, size: " + StringByteLength(XML$) + " bytes."
  
  *MainNode = MainXMLNode(myXML)
  *ChildNode = ChildXMLNode(*MainNode)
  
  ;
  ; This doesn't work - but why!?
  ;
  ExtractXMLList(*ChildNode, Printer()) ;on the child node
  
  ;
  ; This one works
  ;
  Define P.Printer
  ExtractXMLStructure(*ChildNode, @P, Printer)
  
EndIf

;# Debug Output: List
ForEach Printer()
  Debug "Id: " + Printer()\Id
  Debug "Name: " + Printer()\Name
Next

;# Debug Output: Structure
Debug "Id: " + P\Id
Debug "Name: " + P\Name
As you can see I've tried both ExtractXMLList and ExtractXMLStructure but only the latter works. Can somebody illuminate to me why this is?
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: ExtractXMLList behaviour

Post by JHPJHP »

Hi Plan_B,

The XML nodes must have the form described in the InsertXMLList() function.

Code: Select all

Structure Printer
  Id.l
  Name.s
EndStructure

XML.s = "<printers><element><Id>250</Id><Name>Agfa 901</Name></element><element><Id>300</Id><Name>Agfa X300</Name></element></printers>"
myXML = ParseXML(#PB_Any, XML)

If XMLStatus(myXML) = #PB_XML_Success
  NewList Printer.Printer()
  ExtractXMLList(MainXMLNode(myXML), Printer())
  FreeXML(myXML)

  ForEach Printer()
    Debug "Id: " + Printer()\Id
    Debug "Name: " + Printer()\Name
  Next
Else
  Debug "Warning: XML could not be processed."
EndIf
User avatar
Plan_B
New User
New User
Posts: 4
Joined: Thu Nov 23, 2017 5:32 pm

Re: ExtractXMLList behaviour

Post by Plan_B »

Thanks JHPJHP! Wasn't aware it needed to be explicit in that format. For any other noobies who come across the same problem and can not modify the source xml this is how I did it, not sure if it's the best way but it works:

Code: Select all

Structure Printer
  Brand.s
  Model.s
  Image.s
EndStructure

XML.s = "<Printers><Printer><Brand>Epson</Brand><PrinterModel>TM-C3400-012</PrinterModel><Image>img01.jpg</Image></Printer><Printer><Brand>Canon</Brand><PrinterModel>P10</PrinterModel><Image>img02.jpg</Image></Printer></Printers>"
myXML = ParseXML(#PB_Any, XML)

If XMLStatus(myXML) = #PB_XML_Success
  NewList Printer.Printer()
  
  *MainNode = MainXMLNode(myXML)
  *ChildNode = ChildXMLNode(*MainNode)
  
  ;For each <Printer> occurence
  While *ChildNode <> 0
    Select GetXMLNodeName(*ChildNode)
      Case "Printer"
        ;Each element
        *Printer = ChildXMLNode(*ChildNode)
        While *Printer <> 0
          Select GetXMLNodeName(*Printer)
            Case "Brand"
              AddElement(Printer())
              Printer()\Brand = GetXMLNodeText(*Printer)
            Case "PrinterModel"
              Printer()\Model = GetXMLNodeText(*Printer)
            Case "Image"
              Printer()\Image = GetXMLNodeText(*Printer)
          EndSelect
          *Printer = NextXMLNode(*Printer) ;go to next element unter <Printer>
        Wend
    EndSelect
    *ChildNode = NextXMLNode(*ChildNode) ;go to next <Printer> element
  Wend
  
  FreeXML(myXML)

  ForEach Printer()
    Debug "Brand: " + Printer()\Brand
    Debug "Model: " + Printer()\Model
    Debug "Image: " + Printer()\Image
  Next
  
Else
  Debug "Warning: XML could not be processed."
EndIf
Post Reply