Page 1 of 1

Parsing values from specific XML tags.?

Posted: Fri Mar 06, 2020 1:53 pm
by dcr3
Hi. How do I, parse the values between tags into each TextGadget.?

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<..nothing.interesting.here...\><..or.here..\><....\><....\>
<TRN>3249<\TRN><VAR>1597<\VAR><PRF>23<\PRF>
I am only, interested on these last 3 tags.

Code: Select all

Flg=#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
 
 If OpenWindow(0, 0,0,620,72, "P",Flg)
 StringGadget(1,1,12,300,20,"<TRN>"+3249+"<\TRN>"+"<VAR>"+1597+"<\VAR>"+"<PRF>"+23+"<\PRF>")
 TextGadget(2,303, 12, 100, 20, "",#PB_Text_Border)
 TextGadget(3,403, 12, 100, 20, "",#PB_Text_Border)
 TextGadget(4,503, 12, 100, 20, "",#PB_Text_Border)
    
 ButtonGadget(5, 12, 48, 100, 20, "SetVal")
 EndIf
  
Procedure FNT();OpenFile. 
  ;?
  ;?
  ;?
  
  SetGadgetText(2,t$+TRN)
  SetGadgetText(3,t$+VAR)
  SetGadgetText(4,t$+PRF)
EndProcedure

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      AppQuit = 1

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()

        Case 4;Button "SetVal"
        FNT()
      EndSelect

  EndSelect
Until AppQuit

Re: Parsing values from specific XML tags.?

Posted: Fri Mar 06, 2020 2:54 pm
by kenmo
Hi, here's a starting point...

Code: Select all

XMLText.s = "<main><TRN>3249</TRN><VAR>1597</VAR><PRF>23</PRF></main>"
ParseXML(0, XMLText)

*Main = MainXMLNode(0)

TRN = XMLNodeFromPath(*Main, "TRN")
Text.s = GetXMLNodeText(TRN)
Debug Text
; use SetGadgetText(Gadget, Text) here...

VAR = XMLNodeFromPath(*Main, "VAR")

PRF = XMLNodeFromPath(*Main, "PRF")

Re: Parsing values from specific XML tags.?

Posted: Fri Mar 06, 2020 6:41 pm
by dcr3
kenmo, Thanks for trying to help, but these 3 tags, are outside of the main tag. :?:

e.g
<main>..</>..</>..</main><TRN>3249</TRN><VAR>1597</VAR><PRF>23</PRF>

For it to work I have to edit the xml file, cut and paste these 3 tags
inside the main tag.

Re: Parsing values from specific XML tags.?

Posted: Fri Mar 06, 2020 7:02 pm
by srod
You cannot have nodes outside of the main node in a well formed XML document. The parser will effectively ignore them if they are at the end of the document.

Re: Parsing values from specific XML tags.?

Posted: Fri Mar 06, 2020 7:29 pm
by dcr3
I understand, in an ideal world that would have been the case, but they were deliberately
put oustide the main node. :mrgreen: The question is. Is there any other way of parsing those tags. :idea:

Re: Parsing values from specific XML tags.?

Posted: Fri Mar 06, 2020 7:40 pm
by srod
Open as a text file for reading and it shouldn't be too hard to find those tags. Make sure you use ReadStringFormat() to identify the encoding and use the resulting value when using ReadString() etc.

Re: Parsing values from specific XML tags.?

Posted: Fri Mar 06, 2020 7:52 pm
by dcr3
Thanks for tip ReadStringFormat(). I will try it.

Re: Parsing values from specific XML tags.?

Posted: Sat Mar 07, 2020 8:00 am
by Marc56us
dcr3 wrote:I understand, in an ideal world that would have been the case, but they were deliberately
put oustide the main node. :mrgreen: The question is. Is there any other way of parsing those tags. :idea:
FindString()
or
RegularExpression

Re: Parsing values from specific XML tags.?

Posted: Sat Mar 07, 2020 8:58 am
by TI-994A
dcr3 wrote:...parse the values between tags into each TextGadget...
Something like this:

Code: Select all

Flg = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered

If OpenWindow(0, 0, 0, 620, 72, "P", Flg)
  
  StringGadget(1, 1, 12, 300, 20, "<TRN>" +3249 + "</TRN>" + 
                                  "<VAR>" + 1597 + "</VAR>" + 
                                  "<PRF>" + 23 + "</PRF>")
  
  TextGadget(2, 303, 12, 100, 20, "", #PB_Text_Border)
  TextGadget(3, 403, 12, 100, 20, "", #PB_Text_Border)
  TextGadget(4, 503, 12, 100, 20, "", #PB_Text_Border)
  
  ButtonGadget(5, 12, 48, 100, 20, "SetVal")
  
EndIf

Procedure.s GetXMLNodeValue(xmlNodeTag.s, xmlText.s)
  
  xmlNodeValue.s = ""
  valueOffset = Len(xmlNodeTag) + 2
  openTag.s = "<" + UCase(xmlNodeTag) + ">"
  closeTag.s = "</" + UCase(xmlNodeTag) + ">"  
  
  For i = 0 To Len(xmlText) - 1    
    valueStartIndex = FindString(UCase(xmlText), openTag)
    If valueStartIndex
      valueEndIndex = FindString(UCase(xmlText), closeTag)
      If valueEndIndex
        valueStartIndex + valueOffset
        xmlNodeValue = Mid(xmlText, valueStartIndex, valueEndIndex - valueStartIndex)
        Break
      EndIf
    EndIf        
  Next i
  
  ProcedureReturn xmlNodeValue
  
EndProcedure

Procedure FNT()  ;OpenFile.
  
  xmlText.s = GetGadgetText(1)
  Dim xmlNodetag.s(2)
  xmlNodetag(0) = "TRN"
  xmlNodetag(1) = "Var"
  xmlNodetag(2) = "prf"
  
  For i = 0 To 2
    xmlNodeValue$ = GetXMLNodeValue(xmlNodetag(i), xmlText)
    SetGadgetText(2 + i, t$ + xmlNodeValue$)
  Next i
  
EndProcedure

Repeat
  
  Select WaitWindowEvent()
      
    Case #PB_Event_CloseWindow
      AppQuit = 1
      
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case 5  ;Button "SetVal"
          FNT()
      EndSelect
      
  EndSelect
  
Until AppQuit

Re: Parsing values from specific XML tags.?

Posted: Sat Mar 07, 2020 11:22 am
by dcr3
Hi,TI-994A. :) Thanks . Neat . 8)


I am using this one. More the merrier.

Code: Select all

Global TRN.s,VAR.s,PRF.s ;Tags to look for
Procedure FindXmlTags(Str1.s,str2.s,Str3.s)
  
 TRN1 = FindString(Str1.s, "<TRN>", 1)
 TRN2 = FindString(Str1.s,"</TRN>", 1)   
 TRN1 =TRN1 + 5
 TRN2 =TRN2 - 1
   
For x=TRN1 To TRN2
    pTRN.s = Mid(Str1.s,x, 1)   
    TRN.s =TRN.s+ pTRN.s   
Next 
 
 VAR1 = FindString(Str2.s, "<VAR>", 1)
 VAR2 = FindString(Str2.s,"</VAR>", 1)   
 VAR1 = VAR1 + 5
 VAR2 = VAR2 - 1
   
For y=VAR1 To VAR2
    pVAR.s = Mid(Str2.s,y, 1)   
    VAR.s=VAR.s + pVAR.s  
Next
  
  PRF1 = FindString(Str2.s, "<PRF>", 1)
  PRF2 = FindString(Str2.s,"</PRF>", 1)   
  PRF1 = PRF1 + 5
  PRF2 = PRF2 - 1
  
For z=PRF1 To PRF2
    pPRF.s = Mid(Str2.s,z, 1)   
    PRF.s=PRF.s  + pPRF.s
Next

EndProcedure

Procedure FNT()  ;OpenFile.
 
  xmlText.s = GetGadgetText(1)
  
  FindXmlTags(xmlText.s,xmlText.s,xmlText.s)
  
  SetGadgetText(2,TRN.s)
  SetGadgetText(3,VAR.s)
  SetGadgetText(4,PRF.s)
  
EndProcedure
Flg = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered

If OpenWindow(0, 0, 0, 620, 72, "P", Flg)
  StringGadget(1, 1, 12, 300, 20, "<TRN>" +3249 + "</TRN>" +
                                  "<VAR>" + 1597 + "</VAR>" +
                                  "<PRF>" + 23 + "</PRF>")

  TextGadget(2, 303, 12, 100, 20, "", #PB_Text_Border)
  TextGadget(3, 403, 12, 100, 20, "", #PB_Text_Border)
  TextGadget(4, 503, 12, 100, 20, "", #PB_Text_Border)
  ButtonGadget(5, 12, 48, 100, 20, "SetVal")
 
EndIf

Repeat
 
  Select WaitWindowEvent()
     
    Case #PB_Event_CloseWindow
      AppQuit = 1
     
    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect
     
    Case #PB_Event_Gadget
      Select EventGadget()
         
        Case 5  ;Button "SetVal"
         FNT()
      EndSelect
     
  EndSelect
 
Until AppQuit