0

XML Parsing

Hi,

I am new to TV widgets and I don't have much idea. I want to read an XML file stored on local disc and display it on a JPG. How can we access the XML. Is there any such DOM API with the TV Widgets. Also, is XMLHTTPRequest as in Yahoo Widgets supported here on TV Widget?

Riyaz

by
7 Replies
  • Hi Riyaz,

    You can try the following -
    CODE
     
    // read the myxmlfile.xml
    var myXMLdata = filesystem.readFile('Javascript/myxmlfile.xml');
    log("My XML file data : " + myXMLdata);

    // For parsing
    // Retrieve element value
    /* XMF data -
    <?xml version="1.0" encoding="utf-8"?>
    <my-data>
    <element1 name="fred" size="200">
    This is some text
    </element1>
    </my-data>
    */
    try {
    doc = XMLDOM.parse( myXMLdata );

    // Retrieve element value
    myelementValue = doc.evaluate( "string(my-data/element1)" );
    log("myelementValue: " + myelementValue);

    } catch (e) {
    print(e);
    }


    Yes. XMLHttpRequest is there in Yahoo TV Widgets. Please read pdf, page 326 onwards in YWE_Developer_Guide.pdf.

    Regards,
    Azhar

    QUOTE (Md Riyaz @ Jan 29 2010, 06:39 AM) <{POST_SNAPBACK}>
    Hi,

    I am new to TV widgets and I don't have much idea. I want to read an XML file stored on local disc and display it on a JPG. How can we access the XML. Is there any such DOM API with the TV Widgets. Also, is XMLHTTPRequest as in Yahoo Widgets supported here on TV Widget?

    Riyaz
    0
  • Thanks Azhar,

    This is working. I can now access the nodes and attributes. One more, I couldn't find in the reference manual is how to display an Image on the widget. I want to display a local image stored at some folder in the /devwidget, at center of the screen. I see that there's an example of
    var myImage = KONtx.element.Image({ ... etc in the manual but how to use it within KONtx.Class or outside this class is not mentioned. I would really appreciate for the help.

    Riyaz
    0
  • Hello Riyaz,

    CODE
     
    // Change the 'src' property to point to local or remote image.
    var myimage = new KONtx.element.Image({
    // set to remote path
    src: "http://images.smh.com.au/2010/01/27/1068848/pittjolie-420x0.jpg",
    //"image/960x540/Forest.png", <= this is local path
    // for devwidgets, you can try path, "../../myimage.jpg"
    manageWaitIndicator: true,
    styles: {
    vAlign: "center",
    hAlign: "center"
    }
    }).appendTo(this); // 'this' for ex. can be a fullscreen view.


    As you can see the local path is relative to "Contents" folder of your widget. Thus, one can use "../.." to access to devwidgets. But, such folder i.e. devwidgets will not exist on OEM devices (TV) and accessing such folder will result in error on OEM (TV). This works though on simulator. You should load images present in your Contents folder of your widgets or load them from remote sites.

    Regards,
    Azhar

    QUOTE (Md Riyaz @ Feb 2 2010, 07:36 AM) <{POST_SNAPBACK}>
    Thanks Azhar,

    This is working. I can now access the nodes and attributes. One more, I couldn't find in the reference manual is how to display an Image on the widget. I want to display a local image stored at some folder in the /devwidget, at center of the screen. I see that there's an example of
    var myImage = KONtx.element.Image({ ... etc in the manual but how to use it within KONtx.Class or outside this class is not mentioned. I would really appreciate for the help.

    Riyaz
    0
  • Hi Azhar,

    Thanks I am now able to display image as per my requirement. I am still stuck up with displaying the XML values after making a for loop to parse the data. I am not getting any clues as how to debug. I don't get any log or print statements output. I would appreciate if you can help me with an example as how to place values from an XML onto the background display after retreving data from the XML.

    Thanks,
    Riyaz
    0
  • Is is possible to change the XML over to JSON? JSON is much more stable on the platform.

    We have run into some issues with very large XML files -> they will crash the parser. If you are calling the XML document from a web service, you could send it through Yahoo! Pipes to get it converted over to JSON.

    Yahoo! Pipes XML2JSON Converter Example
    CODE
    //Common XML URL
    var xml_url = "http://twitter.com/statuses/public_timeline.xml";

    //Yahoo! Pipes JSON URL
    var json_url = 'http://pipes.yahoo.com/pipes/pipe.run?URL='+escape(xml_url)+'&_id=3aaeb62c1cf2cdce00ef260c4dc3da7c&_render=json';
    //Will return a JSON Object that can be parsed via JSON.parse()


    XML Feed
    http://twitter.com/statuses/public_timeline.xml

    XML2JSON PipeFeed
    http://pipes.yahoo.com/pipes/pipe.run?URL=...mp;_render=json
    0
  • ok. Well I went through JSON and found that I can convert my XML into JSON. There are many utilities available to convert xml to json. Well, thanks for the help and will keep updated. By the way thanks for every help. Is opacity property working on png background displayed. I used it but I couldn't see that the background png isn't changing its opacity.

    Riyaz
    0
  • Opacity definitely does work. Provide a code sample and I can point out the issue.

    -Jeremy
    0

Recent Posts

in Getting Started / Beginners - Yahoo! TV Widgets