Parsing values from specific XML tags.?

Just starting out? Need help? Post your questions and find answers here.
dcr3
Enthusiast
Enthusiast
Posts: 188
Joined: Fri Aug 04, 2017 11:03 pm

Parsing values from specific XML tags.?

Post 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
User avatar
kenmo
Addict
Addict
Posts: 2078
Joined: Tue Dec 23, 2003 3:54 am

Re: Parsing values from specific XML tags.?

Post 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")
dcr3
Enthusiast
Enthusiast
Posts: 188
Joined: Fri Aug 04, 2017 11:03 pm

Re: Parsing values from specific XML tags.?

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Parsing values from specific XML tags.?

Post 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.
I may look like a mule, but I'm not a complete ass.
dcr3
Enthusiast
Enthusiast
Posts: 188
Joined: Fri Aug 04, 2017 11:03 pm

Re: Parsing values from specific XML tags.?

Post 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:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Parsing values from specific XML tags.?

Post 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.
I may look like a mule, but I'm not a complete ass.
dcr3
Enthusiast
Enthusiast
Posts: 188
Joined: Fri Aug 04, 2017 11:03 pm

Re: Parsing values from specific XML tags.?

Post by dcr3 »

Thanks for tip ReadStringFormat(). I will try it.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Parsing values from specific XML tags.?

Post 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
User avatar
TI-994A
Addict
Addict
Posts: 2791
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Parsing values from specific XML tags.?

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
dcr3
Enthusiast
Enthusiast
Posts: 188
Joined: Fri Aug 04, 2017 11:03 pm

Re: Parsing values from specific XML tags.?

Post 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

Post Reply