sivasrinivask 0 Newbie Poster

hi friends,
i am able to parse the xml file and retrieve the corresponding tag values. Now how can i send the retrieved values from python function to the c functions.
And also can any one help me in retrieving all the sub tag values from a particular action tag.
I am very thankful for helping me.

Thanks and Regards,
siva.

c file:

#ifdef _DEBUG
  #undef _DEBUG
  #include "python.h"
  #define _DEBUG
#else
  #include "python.h"
#endif
#include <Python.h>


int main(int argc, char *argv[])
{
    int i;
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

    if (argc < 3) 
    {
        printf("Usage: exe_name python_source function_namen");
        return 1;
    }

    // Initialize the Python Interpreter
    Py_Initialize();

    // Build the name object
    pName = PyString_FromString(argv[1]);

    // Load the module object
    pModule = PyImport_Import(pName);

    // pDict is a borrowed reference 
    pDict = PyModule_GetDict(pModule);

    // pFunc is also a borrowed reference 
    pFunc = PyDict_GetItemString(pDict, argv[2]);

    if (PyCallable_Check(pFunc)) 
    {
        // Prepare the argument list for the call
        if( argc > 3 )
        {
                pArgs = PyTuple_New(argc - 3);
                for (i = 0; i < argc - 3; i++)
                {
                    pValue = PyInt_FromLong(atoi(argv[i + 3]));
                    if (!pValue)
                    {
                        PyErr_Print();
                         return 1;
                    }
                    PyTuple_SetItem(pArgs, i, pValue);    
                }

                pValue = PyObject_CallObject(pFunc, pArgs);

                if (pArgs != NULL)
                {
                    Py_DECREF(pArgs);
                }
        } else
        {
                pValue = PyObject_CallObject(pFunc, NULL);

        }

        if (pValue != NULL) 
        {
            //printf("Return of call : %dn", PyInt_AsLong(pValue));
            printf("Return of call : %sn", PyString_FromString(pValue));
            Py_DECREF(pValue);
        }
        else 
        {
            PyErr_Print();
        }
    } else 
    {
        PyErr_Print();
    }

    // Clean up
    Py_DECREF(pModule);
    Py_DECREF(pName);

    // Finish the Python Interpreter
    Py_Finalize();

    return 0;
}

python file:

import sys, string
from xml.dom import minidom, Node

def walk(parent, outFile, level):                               # [1]
    for node in parent.childNodes:
        if node.nodeType == Node.ELEMENT_NODE:
            # Write out the element name.
            printLevel(outFile, level)
            print "level :",level
            outFile.write('Element: %sn' % node.nodeName)

            # Walk over any text nodes in the current node.
            content = []                                        # [3]
            for child in node.childNodes:
                if child.nodeType == Node.TEXT_NODE:
                        content.append(child.nodeValue)

            if content:
                strContent = ''.join(content)
                printLevel(outFile, level)
                outFile.write('Content: "')

                outFile.write(strContent)
                outFile.write('"n')    
            # Walk the child nodes.
            walk(node, outFile, level+1)

def printLevel(outFile, level):
    for idx in range(level):
        outFile.write('    ')

def run(inFileName):                                            # [5]
    outFile = sys.stdout
    doc = minidom.parse(inFileName)
    rootNode = doc.documentElement
    level = 0
    walk(rootNode, outFile, level)

def main():
    run("client.tcf")
    return 0


if __name__ == '__main__':
    main()

XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<test-case>
       <id>client</id>
       <description>To test the UTEST BACKEND.</description>
       <include>
                <file>siva.mlb</file>
                <file>srinivas.mlb</file>
        </include>
        <state>
                <name>INIT</name>
                <action>
                           <type>SEND</type>
                           <message-id>MSG1</message-id>
                           <message-var>0</message-var>
                           <interface>INF_2</interface>
                 </action>
                 <action>
                            <type>SEND</type>
                            <message-id>MSG2</message-id>
                            <message-var>0</message-var>
                            <interface>INF_1</interface>
                  </action>
                  <action>
                            <type>WAIT</type>
                            <message-id>MSG2</message-id>
                            <message-var>0</message-var>
                            <interface>INF_2</interface>
                            <time-out>1000</time-out>
                        <time-out-state>INDETERMINATE</time-out-state>
                            </action>
                    <action>
                            <type>WAIT</type>
                            <message-id>MSG1</message-id>
                            <message-var>0</message-var>
                            <interface>INF_1</interface>
                            <next-state>PASSED</next-state>
                            <time-out>1000</time-out>
                        <time-out-state>INDETERMINATE</time-out-state>
                   </action>
         </state>
</test-case>