0

Simple XML parser script

Hello

 

I really really struggle to write a custom parser for XML that can be accessed via a webserver. I would like the script to access the XML and parse it. To be able to do stuff as show a number of images/videos specified in the XML. In a specified or random order depending of some attributes in the XML.

 

I have read the documentation about custom parsers, and I still do not know how to write one. I would like a simple sample script that traverse an XML and displays something somewhere or writes something to a log or similar. Something that shows that the custom parser does actually work. The sample script in the documentation seams to do nothing so I do not know if it is working or not.

2 comments

  • 0
    Avatar
    Lyndon

    As far as creating a file on the sd card, you can send any string to a file on the sd card, and then flush the file. 

     

    For example:

    myfile = createobject("roAppendFile", "mylog.txt")

    myfile.sendline("place any string here")

    myfile.flush() 'writes changes to the sd card. 

     

    If you have a sample parser you did, we can show you how to add the creation of a log file to it. 

     

    yahoo weather url:

    http://weather.yahooapis.com/forecastrss?w=116545&u=c&d=4
    

     

    The attached parser pulls information from this type of url. It will store information retrieved in variables mentioned in the readme. So, if you add the variables to your project, the plugin populates the variable with that information.

     

    Plugin readme:

     

     

    These are the variables for the current day values
    
      curr_cond - current weather condition, cloudy
      curr_code - current weather icon code
      curr_temp - temperature
      curr_day - Mon or Fri
      curr_daynum - 4, for example
      curr_month - Jun or Mar
      curr_time - 1:54 pm, for example
    
    And forecasted values for 4 days including the current day as day1 through day4. Each of the values before will be stored in a variable of the same name. 
    
       day1 - for example, Tuesday
       daynum1 - for example, 4
       month1 - Mar
       date1 - 4 Mar 2014
       low1 - 9
       high1 - 23
       cond1 - This is weather conidtion. For example, Mostly Clear, Cloudy, etc
       code1 - This is the weather code that corresponds to the little icons you can also download from yahoo
    

     

  • 0
    Avatar
    Tobias Malm

    Hi and thanks for the reply!

     

    Since my post I have managed to print the content of an XML to a file on the player with the following script:

        

    Sub rss(xmlFileName$ as String, itemsByIndex as object, itemsByTitle as Object, userVariables As Object)

    debug=false

    systemTime = CreateObject("roSystemTime")
    myTime = systemTime.GetLocalDateTime()
    month = mid(str(mytime.getmonth()), 2)
    day = mid(str(mytime.getday()),2)
    year = mid(str(mytime.getyear()),2)
    date = month+day+year

    logname = date+"_log.txt"
    if debug print logname

    f = CreateObject("roAppendFile", logname)
    if type(f) <> "roAppendFile" then f = CreateObject("roCreateFile", logname)
    print #f, "PARSE XML!!!!"

    root = CreateObject("roXMLElement")
    if not root.Parse(ReadAsciiFile(xmlFileName$)) then
    print #f, "xml read failed"
    else
    print #f, "xml read successfull"

    PrintXML(root, 0, f)
    endif

    DeleteFile(xmlfilename$)

    f = invalid
    if debug print "finished"

    end Sub

    Sub PrintXML(element As Object, depth As Integer, file As Object)
    print #file, tab(depth * 3);"Name: ";element.GetName()
    if not element.GetAttributes().IsEmpty() then
    print #file, tab(depth * 3);"Attributes: ";

    for each a in element.GetAttributes()
    print #file, a; " = ";left(element.GetAttributes()[a], 20);

    if element.GetAttributes().IsNext() then print ", ";
    end for

    print #file
    end if

    if element.GetText()<>invalid then
    print #file, tab(depth * 3);"Contains Text: ";left(element.GetText(), 40)
    end if

    if element.GetChildElements()<>invalid
    print #file, tab(depth * 3);"Contains roXMLList:"

    for each e in element.GetChildElements()
    PrintXML(e, depth + 1, file)
    end for
    end if
    print #file
    end sub

     

    And I have managed to create a very simple parser script that replaces the input RSS data with something:

     

    Sub rss(xmlFileName$ as String, itemsByIndex as Object, itemsByTitle as Object, userVariables As Object)

    itemsByIndex.Clear()

    xml = CreateObject("roXMLElement")

    if not xml.Parse(ReadAsciiFile(xmlFileName$)) then
    print "xml read failed"
    else
    count = -1
    itemsByIndex.Clear()
    if type(xml.channel.item) = "roXMLList" then
    for each item in xml.channel.item
    count=count+1

    if type(itemsByIndex) = "roArray" then
    itemsByIndex.Push("foobar")
    endif

    next
    endif
    DeleteFile(xmlFileName$)
    endif

    end Sub

     

    This is where I get stuck, I want to parse an XML and feed the parsed data to a Live Text object. The XML contains info about images and movies (where they are located on a webserver etc.) that I want the Brightsign player to display as a normal MRSS.

     

    Now I know that I can manipulate the parameters, according to your documentation:  itemsByIndex (roArray) and itemsByTitle (roAssociativeArray) but I have only managed to do stuff to itemsByIndex. If I try to do things to itemsByTitle I get a type mismatch in the player and I have no idea why. E.g. the goal is to take the XML, parse it and get the output (on the screen) as if it where a MRSS feed.

Please sign in to leave a comment.