Re: Parsing XML in gnome-shell extension
- From: Amy <mathematical coffee gmail com>
- To: "Jasper St. Pierre" <jstpierre mecheye net>
- Cc: gnome-shell-list <gnome-shell-list gnome org>, alok barsode <alokbarsode gmail com>
- Subject: Re: Parsing XML in gnome-shell extension
- Date: Fri, 9 Nov 2012 09:10:08 +1000
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
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()`.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]