Пример #1
0
int
main (int argc, char **argv)
{
  GError *error = NULL;
  RestXmlParser *parser;
  RestXmlNode *root, *node;
  char *xml;

  g_thread_init (NULL);
  g_type_init ();

  parser = rest_xml_parser_new ();

  root = rest_xml_parser_parse_from_data (parser, TEST_XML, strlen (TEST_XML));
  g_assert (root);

  xml = rest_xml_node_print (root);

  g_assert (xml);

  if (strcmp (TEST_XML, xml))
    {
      g_error ("Generated output for parsed XML does not match:\n"
               "in:  %s\n"
               "out: %s\n",
               TEST_XML, xml);
    }

  rest_xml_node_unref (root);

  root = rest_xml_node_add_child (NULL, "node0");
  rest_xml_node_add_attr (root, "a00", "v00");
  rest_xml_node_add_attr (root, "a01", "v01");

  node = rest_xml_node_add_child (root, "node1");
  rest_xml_node_add_attr (node, "a10", "v10");

  node = rest_xml_node_add_child (root, "node1");
  rest_xml_node_add_attr (node, "a10", "v10");

  rest_xml_node_set_content (root, "Cont0");

  xml = rest_xml_node_print (root);

  g_assert (xml);

  if (strcmp (TEST_XML, xml))
    {
      g_error ("Generated output for constructed XML does not match:\n"
               "in:  %s\n"
               "out: %s\n",
               TEST_XML, xml);
    }

  rest_xml_node_unref (root);

  return 0;
}
Пример #2
0
/**
 * rest_xml_node_print:
 * @node: #RestXmlNode
 *
 * Recursively outputs given node and it's children.
 *
 * Return value: (transfer full): xml string representing the node.
 */
char *
rest_xml_node_print (RestXmlNode *node)
{
  GHashTableIter iter;
  gpointer       key, value;
  GString        *xml = g_string_new (NULL);
  RestXmlNode   *n;

  g_string_append (xml, "<");
  g_string_append (xml, node->name);

  g_hash_table_iter_init (&iter, node->attrs);
  while (g_hash_table_iter_next (&iter, &key, &value))
    g_string_append_printf (xml, " %s =\'%s\'", (char *)key, (char *)value);

  g_string_append (xml, ">");

  g_hash_table_iter_init (&iter, node->children);
  while (g_hash_table_iter_next (&iter, &key, &value))
    {
      char *child = rest_xml_node_print ((RestXmlNode *) value);

      g_string_append (xml, child);
      g_free (child);
    }

  if (node->content)
    g_string_append (xml, node->content);

  g_string_append_printf (xml, "</%s>", node->name);

  for (n = node->next; n; n = n->next)
    {
      char *sibling = rest_xml_node_print (n);

      g_string_append (xml, sibling);
      g_free (sibling);
    }

  return g_string_free (xml, FALSE);
}
Пример #3
0
static gchar *
_construct_upload_atom_xml (GHashTable *fields, gboolean incomplete)
{
  GHashTableIter iter;
  gpointer key, value;
  RestXmlNode *entry = rest_xml_node_add_child (NULL, "entry");
  RestXmlNode *group = rest_xml_node_add_child (entry, "media:group");
  gchar *bare_xml;
  gchar *full_xml;

  rest_xml_node_add_attr (entry, "xmlns", "http://www.w3.org/2005/Atom");
  rest_xml_node_add_attr (entry, "xmlns:media",
                          "http://search.yahoo.com/mrss/");
  rest_xml_node_add_attr (entry, "xmlns:yt",
                          "http://gdata.youtube.com/schemas/2007");

  if (incomplete)
    rest_xml_node_add_child (group, "yt:incomplete");

  g_hash_table_iter_init (&iter, fields);

  while (g_hash_table_iter_next (&iter, &key, &value)) {
    RestXmlNode *node;
    gchar *tag_name;
    const gchar* field_value = value;
    const gchar* field_key = key;

    tag_name = g_strdup_printf ("media:%s", field_key);

    node = rest_xml_node_add_child (group, tag_name);

    if (g_strcmp0 (field_key, "title") == 0 ||
        g_strcmp0 (field_key, "description") == 0)
      rest_xml_node_add_attr (node, "type", "plain");

    if (g_strcmp0 (field_key, "category") == 0)
      rest_xml_node_add_attr (node, "scheme", "http://gdata.youtube.com/"
                              "schemas/2007/categories.cat");

    rest_xml_node_set_content (node, field_value);
  }

  bare_xml = rest_xml_node_print (entry);
  full_xml = g_strdup_printf ("<?xml version=\"1.0\"?>\n%s", bare_xml);

  rest_xml_node_unref (entry);
  g_free (bare_xml);

  return full_xml;
}