PxPlus User Forum

Twitter Twitter Twitter

Author Topic: simple XML processing  (Read 1737 times)

Thomas Bock

  • Diamond Member
  • *****
  • Posts: 177
    • View Profile
simple XML processing
« on: July 13, 2018, 05:27:56 AM »
It would be nice to have the same functionalities for XML as for JSon.
In most cases simple data structures must be processed. It is much easier to iterate over an array than to parse a XML file.

create XML from associative array
DIM(XML_LIST ARR$[All]
create associative array from XML
DIM XML_LOAD ARR$[ALL]=XML$
DIM XMLFILE_LOAD ARR$[ALL]=XMLFile$

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: simple XML processing
« Reply #1 on: July 13, 2018, 06:16:20 PM »
While we could look to doing this, I don't follow why you think parsing in an array is easier than the current XML parsing using *obj/xml.

Here is a comparison of JSON versus XML.

First the JSON data:
{ "countries":[
    { "name":"Canada",
      "capital":"Ottawa" },
    { "name":"USA",
      "capital":"Washington" },
    { "name":"France",
      "capital":"Paris" }
    ] }

Now the code to parse assuming X$ contains the JSON:
Code: [Select]
  dim load array${all}=x$
!
  for n=1 to 100000
  sfx$="countries."+str(n)+"."
  name$=array$[sfx$+"name"]
  if name$="" \
   then break
  print "Country:",name$," capital:",array$[sfx$+"capital"]
  next

Now the XML Data
<country>
 <name>Canada</name>
 <capital>Ottawa</capital>
</country>
<country>
 <name>USA</name>
 <capital>Washington</capital>
</country>
<country>
 <name>France</name>
 <capital>Paris</capital>
</country>

And the code to parse the XML in X$
Code: [Select]
  oXml=new("*obj/xml" for program) ! Create the object
  oXml'Set_XML(x$) ! Load the XML
!
  n=oXml'Nodes
  for n
  oCountry=oXml'Node(n)
  print "Country:",oCountry'Find_node("name")'value$," capital:",oCountry'Find_node("capital")'value$
  next

The total number of lines of code is the same, but the JSON processing is making assumptions that the NAME is not blank to determine the number of entries.
The XML knows the count and by definition will validate missing data whereas the JSON parsing logic would need to be enhanced if it wanted to make sure the capital and name fields were present.

I don't see how parsing XML to an array will make this any easier. 

BTW: We have made a few changes to improve the XML object for the next release such as a Value$(tagname$) method which combines the Find_Node and Value$ property and you can pass the XML when creating the object.

This will further simplify the code, when using PxPlus 2019, to:
Code: [Select]
  oXml=new("*obj/xml",x$ for program)
!
  n=oXml'Nodes
  for n
  oCountry=oXml'Node(n)
  print "Country:",oCountry'Value$("name")," capital:",oCountry'Value$("capital")
  next
« Last Edit: July 13, 2018, 07:32:52 PM by Mike King »
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com