viewpoint-particle

Author Topic: XML Parsing Difficulties  (Read 163 times)

bmclellan

  • Newbie
  • *
  • Posts: 48
XML Parsing Difficulties
« on: April 09, 2020, 03:16:56 pm »
Hello, I have an XML document that looks like below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Stores xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <store storeID="877389504">
      <dynamicMenu>Yes</dynamicMenu>
      <dynamicExt>11111</dynamicExt>
      <dynamicHours>no</dynamicHours>
   </store>
   <store storeID="877389504">
      <dynamicMenu>no</dynamicMenu>
      <dynamicExt>no</dynamicExt>
      <dynamicHours>18</dynamicHours>
   </store>
</Stores>

I am attempting to read this into this into an array to use with the ReportView control. Unfortunately I am having the worst time trying to get the values out of here.
My goal is to have the output
StoreID       dynamicMenu dynamicExt dynamicHours
877389504  yes                11111         yes
877389504  no                 no               18

I think I've tried just about every combination possible, but what I'd like to do is something like below:

strPageContent = fileget(IntakeFileName)   ;This is the XML above
xmlDoc = ObjectCreate("MSXML2.DOMDocument.3.0")
xmlDoc.SetProperty('SelectionLanguage','XPath')
xmlDoc.async = @FALSE
xmlDoc.loadxml( strPageContent )
If xmlDoc.parseError.errorCode != 0
  Message('Invalid XML Files - parseError', xmlDoc.parseError.reason);  ERROR: The following tags were not closed: assembly, file.
  exit
end if

oRoot = xmlDoc.selectNodes('.//store[contains(@storeID,"")]')
StoreNo = oRoot.item(TotalNodes).getAttribute('storeID')

Then get the remaining 3 elements as well, but I'm not sure how to do this easily.
      <dynamicMenu>no</dynamicMenu>
      <dynamicExt>no</dynamicExt>
      <dynamicHours>18</dynamicHours>

I made the mistake of taking the storeNo and then putting that into a
xmlDoc.selectSingleNode(QueryStr) and grabbing the child nodes from that oPC=oStoreNode.childNodes but that just adds too much overhead by searching the doc all over again to get out the child nodes.

Hope someone can help, maybe it's just being so cooped up these days, but soo frustrating!

Thanks everyone!
Barry


JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: XML Parsing Difficulties
« Reply #1 on: April 10, 2020, 07:01:01 pm »
See if this gets you back on track...I always strip namespace stuff and the document declaration.   Maybe there is a way to make that work but never has for me and has never been worth the time to me to figure it out.  I assume getting things in an array won't be an issue for you and that the XML stuff was the problem.  If I am wrong let me know.

Jim

Code: Winbatch

strPageContent = fileget(IntakeFileName)   ;This is the XML above

strPageContent = ItemRemove(1,xml,">")
strPageContent = StrReplace(xml,' xmlns:xsi="http:://www.w3.org/2001/XMLSchema-instance',"")

xmlDoc = ObjectCreate("MSXML2.DOMDocument.3.0")
xmlDoc.SetProperty('SelectionLanguage','XPath')
xmlDoc.async = @FALSE
xmlDoc.loadxml( strPageContent )
If xmlDoc.parseError.errorCode != 0
  Message('Invalid XML Files - parseError', xmlDoc.parseError.reason);  ERROR: The following tags were not closed: assembly, file.
  exit
end if

oRoot = xmlDoc.selectNodes('.//store[contains(@storeID,"")]')
ForEach oNode in oRoot
  StoreNo = oNode.getAttribute('storeID')
  Menu    = oNode.SelectSingleNode("dynamicMenu").Text
  Ext     = oNode.SelectSingleNode("dynamicExt").Text
  Hours   = oNode.SelectSingleNode("dynamicHours").Text
  Message("Store",StoreNo:@CRLF:Menu:@CRLF:Ext:@CRLF:Hours)
Next

ObjectClose(xmlDoc)

 

bmclellan

  • Newbie
  • *
  • Posts: 48
Re: XML Parsing Difficulties
« Reply #2 on: April 11, 2020, 02:33:01 pm »
That was it Jim!!

Thanks so much, I could not wrap my head around it for the life of me!!



Barry

JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: XML Parsing Difficulties
« Reply #3 on: April 11, 2020, 02:43:25 pm »
Glad to hear it.

jim