Re: Parsing XML in gnome-shell extension

Don't forget you have to remove the `<?xml version=....?>` part of the XML before it will parse properly:

    content = Shell.get_file_contents_utf8_sync('/path/to/file'); // or your preferred file-reading function
    // remove <?xml version=...?> (        
    content = content.replace(/^<\?xml\s+version\s*=\s*(["']).*?\1[^?]*\?>/, '');
    xml = new XML(content);

Then you can do stuff like `xml.tagname` to get a list of children with that tag name (use `xml.tagname.length()` to get the number of nodes, `xml.tagname[i]` to iterate through).

You can also use double dot `xml..tagname` to find any descendant with 'tagname', not just the immediate children.

And `xml.tagname.@attributename` (e.g. xml.a.@href) to retrieve that attribute.
But when you do that the output is not a string - you need to use xml.a.@href.toString() to convert it.
To get the text of a tag (e.g. <a href="" Text</a>) you can use `node.text()`.

On 9 November 2012 00:35, Jasper St. Pierre <jstpierre mecheye net> wrote:
You can use E4X to parse XML. It's a fancy language thing that's a bit hard to get used to, and it's deprecated, but it works.

On Thu, Nov 8, 2012 at 7:12 AM, alok barsode <alokbarsode gmail com> wrote:

I am writing a gnome shell extension for oFono. 
I need to know a way to parse an XML file in the extension.

I am using gnome-shell 3.4 


gnome-shell-list mailing list
gnome-shell-list gnome org


gnome-shell-list mailing list
gnome-shell-list gnome org

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]