XmlParser


Object Hierarchy:

Object hierarchy for XmlParser

Description:

[ CCode ( type_id = "rest_xml_parser_get_type ()" ) ]
public class XmlParser : Object

Example: XML-Dump:

public string generate_attrs_output (HashTable<string, string> attrs) {
StringBuilder builder = new StringBuilder ();
builder.append ("{ ");

builder.append ("}");
attrs.foreach ((key, value) => {
builder.append (key);
builder.append_c (':');
builder.append (value);
});

return (owned) builder.str;
}

void xml_node_output (Rest.XmlNode node, int depth) {
do {
// node.attrs is bound as <void*, void*> (Wed Aug 28, 2013)
string attrs_output = generate_attrs_output ((HashTable<string, string>) node.attrs);
print ("%*s[%s, %s, %s]\n",
depth, "", node.name, (node.content != null)? node.content : "NULL",
attrs_output);

// Bound as <void*, void*> (Wed Aug 28, 2013)
((HashTable<string, Rest.XmlNode>) node.children).foreach ((name, child) => {
print ("%*s%s - >\n", depth, "", child.name);
xml_node_output (child, depth + 4);
});
} while ((node = node.next) != null);
}

public static int main (string[] args) {
if (args.length != 2) {
stderr.puts ("$ dump-xml <FILENAME>\n");
return 1;
}

string data;
size_t length;

try {
FileUtils.get_contents (args[1], out data, out length);
} catch (FileError e) {
stderr.printf ("%s\n", e.message);
return 1;
}

Rest.XmlParser parser = new Rest.XmlParser ();
Rest.XmlNode node = parser.parse_from_data (data, length);
if (node == null) {
print ("Cannot parse document\n");
return -1;
}

xml_node_output (node, 0);
return 0;
}

valac --pkg rest-0.7 dump-xml.vala

Example: Fireeagle (outdated):

// README: Fire Eagle has closed as of February 2013.
// This eample won't work anymore. However, the code
// might be useful.
public static int main (string[] args) {
// Create the proxy:
Rest.OAuthProxy proxy = new Rest.OAuthProxy (
"NmUm6hxQ9a4u", // Consumer Key
"t4FM7LiUeD4RBwKSPa6ichKPDh5Jx4kt", // Consumer Secret
"https://fireeagle.yahooapis.com/", // FireEagle endpoint
false);

// First stage authentication, this gets a request token:
try {
proxy.request_token ("oauth/request_token", "oob");
} catch (Error e) {
error ("Cannot request token: %s", e.message);
}

// From the token construct a URL for the user to visit:
print ("Go to https://fireeagle.yahoo.net/oauth/authorize?oauth_token=%s then enter the verification code\n",
proxy.get_token ());

// Read the PIN:
string pin = stdin.read_line ();

// Second stage authentication, this gets an access token:
try {
proxy.access_token ("oauth/access_token", pin);
} catch (Error e) {
error ("Cannot request token: %s", e.message);
}

// Get the user's current location:
Rest.ProxyCall call = proxy.new_call ();
call.set_function ("api/0.1/user");

try {
call.run ();
} catch (Error e) {
error ("Cannot make call: %s", e.message);
}

Rest.XmlParser parser = new Rest.XmlParser ();
Rest.XmlNode root = parser.parse_from_data (
call.get_payload (),
call.get_payload_length ());

Rest.XmlNode node = root.find ("location");
node = node.find ("name");
print ("%s\n", node.content);
return 0;
}

valac --pkg rest-0.7 get-fireeagle-location.vala


Namespace: Rest
Package: rest-0.7

Content:

Creation methods:

Methods:

Inherited Members: