我能够自检一个DBus节点,并获得一些XML,其中包含有关子节点的信息。但是,这需要我解析XML,并且试图使应用程序保持轻量级。我可以使用什么gdbus函数来简单地获得子节点对象名称的列表?
这是获取XML的代码。
#include <stdio.h>
#include <stdlib.h>
#include <gio/gio.h>
int main(int argc,char *argv[])
{
GError *err=NULL;
GVariant *result;
GDBusConnection *c;
const char *xml;
if ((c = g_bus_get_sync(G_BUS_TYPE_SYSTEM,NULL,&err)) == NULL) {
if (err) fprintf(stderr,"g_bus_get error: %s\n",err->message);
exit(1);
} /* if */
result = g_dbus_connection_call_sync(c,"org.bluez","/org/bluez",
"org.freedesktop.DBus.Introspectable",
"Introspect",NULL,G_VARIANT_TYPE("(s)"),
G_DBUS_CALL_FLAGS_NONE,3000,NULL,&err);
if (result==NULL) {
if (err) fprintf(stderr,"gbus_connection_call error: %s\n",
err->message);
exit(1);
} /* if */
g_variant_get(result,"(&s)",&xml);
printf("%s\n",xml);
exit(0);
}
因此上面的代码有效。在返回的XML的深处,有一些元素描述org.bluez对象节点的子级。就我而言,有一个像这样的元素:
<node name="hci0"></node>.
但是,我不想解析XML来找到它。可以使用什么其他gdbus函数来简单地检索org.bluez的子级名称,而无需XML解析器?
答案 0 :(得分:1)
I think your best bet is to use the built-in XML parser. It's how the gdbus introspect
command line tool is implemented.
Call the g_dbus_node_info_new_for_xml function to parse the XML. This give you back a GDBusNodeInfo
, which you must free with g_dbus_node_info_unref()
. The best example I can find of how to use it is here, which parses the XML, then loops through the nodes
element of the struct that's returned.