<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dimuthu's Blog &#187; axiom</title>
	<atom:link href="http://www.dimuthu.org/catagory/axis2c/axiom/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dimuthu.org</link>
	<description>Waiting for your comments</description>
	<lastBuildDate>Wed, 21 Dec 2011 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
  <link>http://www.dimuthu.org</link>
  <url>http://www.dimuthu.org/favicon.ico</url>
  <title>Dimuthu's Blog</title>
</image>
		<item>
		<title>Hello World To Web Services With C And C++</title>
		<link>http://www.dimuthu.org/blog/2009/01/22/hello-world-to-web-services-with-c-and-c/</link>
		<comments>http://www.dimuthu.org/blog/2009/01/22/hello-world-to-web-services-with-c-and-c/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 04:39:27 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[axiom]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/c++]]></category>
		<category><![CDATA[wso2]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[wsf/cpp]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=903</guid>
		<description><![CDATA[Once you have a web service, you can write clients to invoke that service from any language, mostly with the help of a framework written in to that particular language. When it comes to C, the most popular choice is Apache Axis2/C framework. When you are using Axis2/C to write web service clients, you need [...]]]></description>
			<content:encoded><![CDATA[<p>Once you have a web service, you can write clients to invoke that service from any language, mostly with the help of a framework written in to that particular language. When it comes to C, the most popular choice is <a href="http://ws.apache.org/axis2/c/">Apache Axis2/C</a> framework. When you are using Axis2/C to write web service clients, you need to learn about AXIOM which is a easy to use high performing XML model and the service client API which can be used to actually invoke the service. Lets look at the code.</p>
<pre class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;axiom.h&gt;</span>
<span style="color: #339933;">#include &lt;axis2_util.h&gt;</span>
<span style="color: #339933;">#include &lt;axiom_soap.h&gt;</span>
<span style="color: #339933;">#include &lt;axis2_client.h&gt;</span>

axiom_node_t *build_om_payload_for_helloworld_svc<span style="color: #66cc66;">(</span>
    <span style="color: #993333;">const</span> axutil_env_t * env<span style="color: #66cc66;">)</span>;

<span style="color: #993333;">int</span>
main<span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    <span style="color: #993333;">const</span> axutil_env_t *env = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #993333;">const</span> axis2_char_t *address = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axis2_endpoint_ref_t *endpoint_ref = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axis2_options_t *options = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #993333;">const</span> axis2_char_t *client_home = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axis2_svc_client_t *svc_client = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_node_t *payload = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_node_t *ret_node = <span style="color: #000000; font-weight: bold;">NULL</span>;

    <span style="color: #808080; font-style: italic;">/* Set up the environment */</span>
    env = axutil_env_create_all<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"helloworld.log"</span>, AXIS2_LOG_LEVEL_TRACE<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Set end point reference of helloworld service */</span>
    address = <span style="color: #ff0000;">"http://localhost:9090/axis2/services/helloworld"</span>;

    <span style="color: #808080; font-style: italic;">/* Create EPR with given address */</span>
    endpoint_ref = axis2_endpoint_ref_create<span style="color: #66cc66;">(</span>env, address<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Setup options */</span>
    options = axis2_options_create<span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>;
    axis2_options_set_to<span style="color: #66cc66;">(</span>options, env, endpoint_ref<span style="color: #66cc66;">)</span>;
    axis2_options_set_action<span style="color: #66cc66;">(</span>options, env,
                             <span style="color: #ff0000;">"http://ws.apache.org/axis2/c/samples/helloworldString"</span><span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Set up deploy folder. It is from the deploy folder, the configuration is picked up
     * using the axis2.xml file. You need to set the AXIS2C_HOME variable to the axis2/c
     * installed dir.
     */</span>
    client_home = AXIS2_GETENV<span style="color: #66cc66;">(</span><span style="color: #ff0000;">"AXIS2C_HOME"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!client_home || !strcmp<span style="color: #66cc66;">(</span>client_home, <span style="color: #ff0000;">""</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>
        client_home = <span style="color: #ff0000;">"../.."</span>;

    <span style="color: #808080; font-style: italic;">/* Create service client */</span>
    svc_client = axis2_svc_client_create<span style="color: #66cc66;">(</span>env, client_home<span style="color: #66cc66;">)</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!svc_client<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <span style="color: #808080; font-style: italic;">/* reporting the error */</span>
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a>
            <span style="color: #66cc66;">(</span><span style="color: #ff0000;">"Error creating service client, Please check AXIS2C_HOME again<span style="color: #000099; font-weight: bold;">\\</span>n"</span><span style="color: #66cc66;">)</span>;
        AXIS2_LOG_ERROR<span style="color: #66cc66;">(</span>env-&gt;log, AXIS2_LOG_SI,
                        <span style="color: #ff0000;">"Stub invoke FAILED: Error code:"</span> <span style="color: #ff0000;">" %d :: %s"</span>,
                        env-&gt;error-&gt;error_number,
                        AXIS2_ERROR_GET_MESSAGE<span style="color: #66cc66;">(</span>env-&gt;error<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">-1</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #808080; font-style: italic;">/* Set service client options */</span>
    axis2_svc_client_set_options<span style="color: #66cc66;">(</span>svc_client, env, options<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Build the SOAP request message payload using OM API. */</span>
    payload = build_om_payload_for_helloworld_svc<span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>;

    <span style="color: #808080; font-style: italic;">/* Send request */</span>
    ret_node = axis2_svc_client_send_receive<span style="color: #66cc66;">(</span>svc_client, env, payload<span style="color: #66cc66;">)</span>;

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>ret_node<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <span style="color: #808080; font-style: italic;">/* extracting out the content from the response */</span>
        axis2_char_t *om_str = <span style="color: #000000; font-weight: bold;">NULL</span>;
        om_str = axiom_node_to_string<span style="color: #66cc66;">(</span>ret_node, env<span style="color: #66cc66;">)</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>om_str<span style="color: #66cc66;">)</span>
            <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\\</span>nReceived OM : %s<span style="color: #000099; font-weight: bold;">\\</span>n"</span>, om_str<span style="color: #66cc66;">)</span>;
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\\</span>nhelloworld client invoke SUCCESSFUL!<span style="color: #000099; font-weight: bold;">\\</span>n"</span><span style="color: #66cc66;">)</span>;

        AXIS2_FREE<span style="color: #66cc66;">(</span>env-&gt;allocator, om_str<span style="color: #66cc66;">)</span>;
        ret_node = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">}</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">{</span>
        AXIS2_LOG_ERROR<span style="color: #66cc66;">(</span>env-&gt;log, AXIS2_LOG_SI,
                        <span style="color: #ff0000;">"Stub invoke FAILED: Error code:"</span> <span style="color: #ff0000;">" %d :: %s"</span>,
                        env-&gt;error-&gt;error_number,
                        AXIS2_ERROR_GET_MESSAGE<span style="color: #66cc66;">(</span>env-&gt;error<span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"helloworld client invoke FAILED!<span style="color: #000099; font-weight: bold;">\\</span>n"</span><span style="color: #66cc66;">)</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #808080; font-style: italic;">/* freeing the allocated memory */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>svc_client<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        axis2_svc_client_free<span style="color: #66cc66;">(</span>svc_client, env<span style="color: #66cc66;">)</span>;
        svc_client = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>env<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        axutil_env_free<span style="color: #66cc66;">(</span><span style="color: #66cc66;">(</span>axutil_env_t *<span style="color: #66cc66;">)</span> env<span style="color: #66cc66;">)</span>;
        env = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">}</span>

    <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">}</span></pre>
<p>Here is the implementation of the &#8220;build_om_payload_for_helloworld_svc&#8221; function that build the request SOAP message using Axiom/C. Note that axiom_element and axiom_node has one to one association. We use node to to navigate the XML, where as axiom_element to store the data.</p>
<pre class="c"><span style="color: #808080; font-style: italic;">/* build SOAP request message content using OM
           &lt;ns1:greet xmlns:ns1="http://ws.apache.org/axis2/services/helloworld"&gt;
                &lt;text&gt;Hello World&lt;/text&gt;
           &lt;/ns1:greet&gt;
*/</span>
axiom_node_t *
build_om_payload_for_helloworld_svc<span style="color: #66cc66;">(</span>
    <span style="color: #993333;">const</span> axutil_env_t * env<span style="color: #66cc66;">)</span>
<span style="color: #66cc66;">{</span>
    axiom_node_t *helloworld_om_node = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_element_t *helloworld_om_ele = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_node_t *text_om_node = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_element_t *text_om_ele = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axiom_namespace_t *ns1 = <span style="color: #000000; font-weight: bold;">NULL</span>;
    axis2_char_t *om_str = <span style="color: #000000; font-weight: bold;">NULL</span>;

    ns1 =
       axiom_namespace_create<span style="color: #66cc66;">(</span>env, <span style="color: #ff0000;">"http://ws.apache.org/axis2/services/helloworld"</span>,
                               <span style="color: #ff0000;">"ns1"</span><span style="color: #66cc66;">)</span>;
    helloworld_om_ele =
        axiom_element_create<span style="color: #66cc66;">(</span>env, <span style="color: #000000; font-weight: bold;">NULL</span>, <span style="color: #ff0000;">"greet"</span>, ns1, &amp;helloworld_om_node<span style="color: #66cc66;">)</span>;
    text_om_ele =
        axiom_element_create<span style="color: #66cc66;">(</span>env, helloworld_om_node, <span style="color: #ff0000;">"text"</span>, <span style="color: #000000; font-weight: bold;">NULL</span>, &amp;text_om_node<span style="color: #66cc66;">)</span>;
    axiom_element_set_text<span style="color: #66cc66;">(</span>text_om_ele, env, <span style="color: #ff0000;">"Hello World!"</span>, text_om_node<span style="color: #66cc66;">)</span>;
    om_str = axiom_node_to_string<span style="color: #66cc66;">(</span>helloworld_om_node, env<span style="color: #66cc66;">)</span>;

    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>om_str<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\\</span>nSending OM : %s<span style="color: #000099; font-weight: bold;">\\</span>n"</span>, om_str<span style="color: #66cc66;">)</span>;
        AXIS2_FREE<span style="color: #66cc66;">(</span>env-&gt;allocator, om_str<span style="color: #66cc66;">)</span>;
        om_str = <span style="color: #000000; font-weight: bold;">NULL</span>;
    <span style="color: #66cc66;">}</span>
    <span style="color: #b1b100;">return</span> helloworld_om_node;
<span style="color: #66cc66;">}</span></pre>
<p>So lets see how the same thing is done with C++. For C++ we use <a href="http://wso2.org/projects/wsf/cpp">WSO2 WSF/C++</a></p>
<pre class="cpp"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;WSSOAPClient.h&gt;</span>
<span style="color: #339900;">#include &lt;OMElement.h&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;AxisFault.h&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> wso2wsf;

OMElement build_om_payload_for_helloworld_svc<span style="color: #000000;">(</span><span style="color: #000000;">)</span>;

<span style="color: #0000ff;">int</span> main<span style="color: #000000;">(</span><span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
    WSSOAPClient * sc = <span style="color: #0000dd;">new</span> WSSOAPClient<span style="color: #000000;">(</span><span style="color: #666666;">"http://localhost:9090/axis2/services/helloworld"</span><span style="color: #000000;">)</span>;
    sc-&gt;initializeClient<span style="color: #000000;">(</span><span style="color: #666666;">"helloworld_blocking.log"</span>, AXIS2_LOG_LEVEL_TRACE<span style="color: #000000;">)</span>;
    <span style="color: #000000;">{</span>
        <span style="color: #ff0000; font-style: italic;">/* generating the payload */</span>
        OMElement * payload = build_om_payload_for_helloworld_svc<span style="color: #000000;">(</span><span style="color: #000000;">)</span>;

        OMElement * response;
        <span style="color: #0000ff;">try</span>
        <span style="color: #000000;">{</span>
            <span style="color: #ff0000; font-style: italic;">/* invoking the web service */</span>
            response = sc-&gt;request<span style="color: #000000;">(</span>payload, <span style="color: #666666;">"http://ws.apache.org/axis2/c/samples/helloworldString"</span><span style="color: #000000;">)</span>;

            <span style="color: #ff0000; font-style: italic;">/* printing the response */</span>
            <span style="color: #0000ff;">if</span> <span style="color: #000000;">(</span>response<span style="color: #000000;">)</span>
            <span style="color: #000000;">{</span>
                <span style="color: #0000dd;">cout</span> &lt;&lt; endl &lt;&lt; <span style="color: #666666;">"Response: "</span> &lt;&lt; response &lt;&lt; endl;
            <span style="color: #000000;">}</span>
        <span style="color: #000000;">}</span>

        <span style="color: #ff0000; font-style: italic;">/* handling the fault */</span>
        <span style="color: #0000ff;">catch</span> <span style="color: #000000;">(</span>AxisFault &amp; e<span style="color: #000000;">)</span>
        <span style="color: #000000;">{</span>
            <span style="color: #0000ff;">if</span> <span style="color: #000000;">(</span>sc-&gt;getLastSOAPFault<span style="color: #000000;">(</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>
            <span style="color: #000000;">{</span>
                <span style="color: #0000dd;">cout</span> &lt;&lt; endl &lt;&lt; <span style="color: #666666;">"Response: "</span> &lt;&lt; sc-&gt;getLastSOAPFault<span style="color: #000000;">(</span><span style="color: #000000;">)</span> &lt;&lt; endl;
            <span style="color: #000000;">}</span>
            <span style="color: #0000ff;">else</span>
            <span style="color: #000000;">{</span>
                <span style="color: #0000dd;">cout</span> &lt;&lt; endl &lt;&lt; <span style="color: #666666;">"Response: "</span> &lt;&lt; e &lt;&lt; endl;
            <span style="color: #000000;">}</span>
        <span style="color: #000000;">}</span>
        <span style="color: #0000dd;">delete</span> payload;
    <span style="color: #000000;">}</span>
    <span style="color: #0000dd;">delete</span> sc;
<span style="color: #000000;">}</span></pre>
<p>You can see lines of code is reduced a lot. And you can see it from the code to build the request XML as well.</p>
<pre class="cpp"><span style="color: #ff0000; font-style: italic;">/* building the request soap message
   &lt;ns1:greet xmlns:ns1="http://ws.apache.org/axis2/services/helloworld"&gt;
        &lt;text&gt;Hello World&lt;/text&gt;
   &lt;/ns1:greet&gt;
 */</span>
OMElement build_om_payload_for_helloworld_svc<span style="color: #000000;">(</span><span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
    OMNamespace * ns = <span style="color: #0000dd;">new</span> OMNamespace<span style="color: #000000;">(</span><span style="color: #666666;">"http://ws.apache.org/axis2/services/helloworld"</span>, <span style="color: #666666;">"ns1"</span><span style="color: #000000;">)</span>;
    OMElement * payload = <span style="color: #0000dd;">new</span> OMElement<span style="color: #000000;">(</span><span style="color: #0000ff;">NULL</span>,<span style="color: #666666;">"greet"</span>, ns<span style="color: #000000;">)</span>;
    OMElement * child = <span style="color: #0000dd;">new</span> OMElement<span style="color: #000000;">(</span>payload,<span style="color: #666666;">"text"</span>, <span style="color: #0000ff;">NULL</span><span style="color: #000000;">)</span>;
    child-&gt;setText<span style="color: #000000;">(</span><span style="color: #666666;">"Hello World!"</span><span style="color: #000000;">)</span>;

    <span style="color: #0000ff;">return</span> payload;
<span style="color: #000000;">}</span></pre>
<p>WSF/C++ is build on top of Axis2/C. You can see the WSF/C++ API is designed very carefully to make it easy to use without breaking the flexibility provided in the C API. So C++ developers can straightaway use WSF/C++ to develop their web service consumers. Anyway Axis2/C API still has the power of embedding easily in to scripting languages  (Like it is done in WSF/PHP, WSF/Ruby) and probably deploy in legacy systems that doesn&#8217;t support C++ compiled binaries. So you have the options to select the most sutiable one for your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2009/01/22/hello-world-to-web-services-with-c-and-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using AXIOM/C As The XML Object Model</title>
		<link>http://www.dimuthu.org/blog/2008/12/03/using-axiomc-as-the-xml-object-model/</link>
		<comments>http://www.dimuthu.org/blog/2008/12/03/using-axiomc-as-the-xml-object-model/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 16:35:04 +0000</pubDate>
		<dc:creator>dimuthu</dc:creator>
				<category><![CDATA[axiom]]></category>
		<category><![CDATA[axis2/c]]></category>
		<category><![CDATA[DataServices]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Tutorial/Guide]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[AXIOM/C]]></category>
		<category><![CDATA[axiom_node_t*]]></category>
		<category><![CDATA[Buffer]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.dimuthu.org/?p=754</guid>
		<description><![CDATA[In Apache Axis2/C AXIOM is used as the basic object model to represent XML. AXIOM provide a DOM like API that allows to traverse and build the XML very easily. Anyway in underneath, AXIOM is different from DOM, as it has used some techniques to optimize the parsing of the XML as suited specially for [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://ws.apache.org/axis2/c/">Apache Axis2/C</a> AXIOM is used as the basic object model to represent XML. AXIOM provide a DOM like API that allows to traverse and build the XML very easily.</p>
<p>Anyway in underneath, AXIOM is different from DOM, as it has used some techniques to optimize the parsing of the XML as suited specially for SOAP message processing in web services. For an example the SOAP processor can validate a SOAP message by reading only some parts of the SOAP header fields, and if it is not valid, they can completely skip processing the body part. And since AXIOM is designed to built from a stream of data retrieved from a transport, sometimes SOAP processors can validate the message without the need of reading the full stream.</p>
<p>Anyway there should be lot of application that needs this optimization in parsing XMLs. They can easily adapt AXIOM/C to their application. Here is an <a href="http://ws.apache.org/axis2/c/docs/om_tutorial.html">AXIOM/C tutorial</a> that covers both parsing and building XMLs from AXIOM. In this post I&#8217;d like to mention a code that can be used to retrieve an AXIOM from a String (char buffer) which we call as deserialization.</p>
<pre class="c">    axiom_node_t* AXIS2_CALL
    deserialize_my_buffer <span style="color: #66cc66;">(</span>
        <span style="color: #993333;">const</span> axutil_env_t * env,
        <span style="color: #993333;">char</span> *buffer<span style="color: #66cc66;">)</span>
    <span style="color: #66cc66;">{</span>
        axiom_xml_reader_t *reader = <span style="font-weight: bold; color: #000000;">NULL</span>;
        axiom_stax_builder_t *builder = <span style="font-weight: bold; color: #000000;">NULL</span>;
        axiom_document_t *document = <span style="font-weight: bold; color: #000000;">NULL</span>;
        axiom_node_t *payload = <span style="font-weight: bold; color: #000000;">NULL</span>;

        reader = axiom_xml_reader_create_for_memory <span style="color: #66cc66;">(</span>env,
            buffer, axutil_strlen <span style="color: #66cc66;">(</span>buffer<span style="color: #66cc66;">)</span>,
            AXIS2_UTF_8, AXIS2_XML_PARSER_TYPE_BUFFER<span style="color: #66cc66;">)</span>;

        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!reader<span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
            <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
        <span style="color: #66cc66;">}</span>

        builder = axiom_stax_builder_create <span style="color: #66cc66;">(</span>env, reader<span style="color: #66cc66;">)</span>;

        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!builder<span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
            <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
        <span style="color: #66cc66;">}</span>
        document = axiom_stax_builder_get_document <span style="color: #66cc66;">(</span>builder, env<span style="color: #66cc66;">)</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!document<span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
            AXIS2_LOG_ERROR <span style="color: #66cc66;">(</span>env-&gt;log, AXIS2_LOG_SI,
                    <span style="color: #ff0000;">"Document is null for deserialization"</span><span style="color: #66cc66;">)</span>;
            <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
        <span style="color: #66cc66;">}</span>

        payload = axiom_document_get_root_element <span style="color: #66cc66;">(</span>document, env<span style="color: #66cc66;">)</span>;

        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span>!payload<span style="color: #66cc66;">)</span>
        <span style="color: #66cc66;">{</span>
            AXIS2_LOG_ERROR <span style="color: #66cc66;">(</span>env-&gt;log, AXIS2_LOG_SI,
                    <span style="color: #ff0000;">"Root element of the document is not found"</span><span style="color: #66cc66;">)</span>;
            <span style="color: #b1b100;">return</span> <span style="font-weight: bold; color: #000000;">NULL</span>;
        <span style="color: #66cc66;">}</span>
        axiom_document_build_all <span style="color: #66cc66;">(</span>document, env<span style="color: #66cc66;">)</span>;

        axiom_stax_builder_free_self <span style="color: #66cc66;">(</span>builder, env<span style="color: #66cc66;">)</span>;

        <span style="color: #b1b100;">return</span> payload;
    <span style="color: #66cc66;">}</span></pre>
<p>Regardless of the fact this piece of code is been used many time by Axis2 and application that uses Axis2, it has never been identified as a core AXIOM function. I think it is better we have this function as an alternative method to create an axiom.</p>
<pre class="c">axiom_node_t *AXIS2_CALL
axiom_node_create_from_buffer<span style="color: #66cc66;">(</span><span style="color: #993333;">const</span> axutil_env_t *env, axis2_char_t *buffer<span style="color: #66cc66;">)</span>;</pre>
<p>I already suggested this in Axis2/C mailing list and hopefully it will be included from the next release.</p>
<p>Here when we create the axiom tree function from the character buffer, we used &#8220;axiom_xml_reader_create_for_memory&#8221; function. Anyway whenever transport read data stream from wire it always uses the &#8220;axiom_xml_reader_create_for_io&#8221; function.</p>
<pre class="c">    <span style="font-style: italic; color: #808080;">/**
     * This create an instance of axiom_xml_reader to
     * parse a xml document in a buffer. It takes a callback
     * function that takes a buffer and the size of the buffer
     * The user must implement a function that takes in buffer
     * and size and fill the buffer with specified size
     * with xml stream, parser will call this function to fill the
     * buffer on the fly while parsing.
     * @param env environment MUST NOT be NULL.
     * @param read_input_callback() callback function that fills
     * a char buffer.
     * @param close_input_callback() callback function that closes
     * the input stream.
     * @param ctx, context can be any data that needs to be passed
     * to the callback method.
     * @param encoding encoding scheme of the xml stream
     */</span>
    AXIS2_EXTERN axiom_xml_reader_t *AXIS2_CALL
    axiom_xml_reader_create_for_io<span style="color: #66cc66;">(</span>
        <span style="color: #993333;">const</span> axutil_env_t * env,
        AXIS2_READ_INPUT_CALLBACK read_callback,
        AXIS2_CLOSE_INPUT_CALLBACK close_callback,
        <span style="color: #993333;">void</span> *ctx,
        <span style="color: #993333;">const</span> axis2_char_t * encoding<span style="color: #66cc66;">)</span>;</pre>
<p>As you may have noticed it requires us to implement a &#8220;read_callback&#8221; function. Here is an example function prototype to implement this callback.</p>
<pre class="c">    <span style="color: #993333;">int</span> AXIS2_CALL
    some_function<span style="color: #66cc66;">(</span>
            <span style="color: #993333;">char</span> *buffer,
            <span style="color: #993333;">int</span> size,
            <span style="color: #993333;">void</span> *ctx<span style="color: #66cc66;">)</span>;</pre>
<p>This function will be called by the parser as required to parse the XML read from some stream.</p>
<p>So if your application involves reading data from a stream you are always recommended to use this function (i.e. &#8220;axiom_xml_reader_create_for_io&#8221;) instead of &#8220;axiom_xml_read_create_for_buffer&#8221; to create the AXIOM model more effectively.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dimuthu.org/blog/2008/12/03/using-axiomc-as-the-xml-object-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

