<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Adding custom XML tags into the core XML data</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="GStreamer Application Development Manual" HREF="index.html"><LINK REL="UP" TITLE="XML in GStreamer" HREF="chapter-xml.html"><LINK REL="PREVIOUS" TITLE="Loading a GstElement from an XML file" HREF="section-xml-load.html"><LINK REL="NEXT" TITLE="Appendices" HREF="part-appendices.html"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" ><SPAN CLASS="application" >GStreamer</SPAN > Application Development Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="section-xml-load.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 31. XML in <SPAN CLASS="application" >GStreamer</SPAN ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="part-appendices.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="section-xml-custom" >31.3. Adding custom XML tags into the core XML data</A ></H1 ><P > It is possible to add custom XML tags to the core XML created with gst_xml_write. This feature can be used by an application to add more information to the save plugins. The editor will for example insert the position of the elements on the screen using the custom XML tags. </P ><P > It is strongly suggested to save and load the custom XML tags using a namespace. This will solve the problem of having your XML tags interfere with the core XML tags. </P ><P > To insert a hook into the element saving procedure you can link a signal to the GstElement using the following piece of code: </P ><PRE CLASS="programlisting" > xmlNsPtr ns; ... ns = xmlNewNs (NULL, "http://gstreamer.net/gst-test/1.0/", "test"); ... thread = gst_element_factory_make ("thread", "thread"); g_signal_connect (G_OBJECT (thread), "object_saved", G_CALLBACK (object_saved), g_strdup ("decoder thread")); ... </PRE ><P > When the thread is saved, the object_save method will be called. Our example will insert a comment tag: </P ><PRE CLASS="programlisting" > static void object_saved (GstObject *object, xmlNodePtr parent, gpointer data) { xmlNodePtr child; child = xmlNewChild (parent, ns, "comment", NULL); xmlNewChild (child, ns, "text", (gchar *)data); } </PRE ><P > Adding the custom tag code to the above example you will get an XML file with the custom tags in it. Here's an excerpt: </P ><PRE CLASS="programlisting" > ... <gst:element> <gst:name>thread</gst:name> <gst:type>thread</gst:type> <gst:version>0.1.0</gst:version> ... </gst:children> <test:comment> <test:text>decoder thread</test:text> </test:comment> </gst:element> ... </PRE ><P > To retrieve the custom XML again, you need to attach a signal to the GstXML object used to load the XML data. You can then parse your custom XML from the XML tree whenever an object is loaded. </P ><P > We can extend our previous example with the following piece of code. </P ><PRE CLASS="programlisting" > xml = gst_xml_new (); g_signal_connect (G_OBJECT (xml), "object_loaded", G_CALLBACK (xml_loaded), xml); ret = gst_xml_parse_file (xml, "xmlTest.gst", NULL); g_assert (ret == TRUE); </PRE ><P > Whenever a new object has been loaded, the xml_loaded function will be called. This function looks like: </P ><PRE CLASS="programlisting" > static void xml_loaded (GstXML *xml, GstObject *object, xmlNodePtr self, gpointer data) { xmlNodePtr children = self->xmlChildrenNode; while (children) { if (!strcmp (children->name, "comment")) { xmlNodePtr nodes = children->xmlChildrenNode; while (nodes) { if (!strcmp (nodes->name, "text")) { gchar *name = g_strdup (xmlNodeGetContent (nodes)); g_print ("object %s loaded with comment '%s'\n", gst_object_get_name (object), name); } nodes = nodes->next; } } children = children->next; } } </PRE ><P > As you can see, you'll get a handle to the GstXML object, the newly loaded GstObject and the xmlNodePtr that was used to create this object. In the above example we look for our special tag inside the XML tree that was used to load the object and we print our comment to the console. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="section-xml-load.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="part-appendices.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Loading a GstElement from an XML file</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="chapter-xml.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Appendices</TD ></TR ></TABLE ></DIV ></BODY ></HTML >