XML Reader

Making & changing box plugins and external apps
Post Reply
jbrumberg
Posts: 5
Joined: Mon Jul 12, 2010 7:02 pm

XML Reader

Post by jbrumberg »

Are there any good examples of "standalone" XML readers in the OpenViBE code? I am attempting to read an XML file in a new plugin, but it would be helpful to have a template to go by using OpenViBE syntax.

Thanks
Jon

yrenard
Site Admin
Posts: 645
Joined: Fri Sep 01, 2006 3:39 pm
Contact:

Re: XML Reader

Post by yrenard »

Dear Jon,

Thank you for your interest in OpenViBE and welcome on this board.
Would you introduce yourself a little ?

Regarding the XML issue you are facing, the OpenViBE module adopts a SAX approach to parse the XML files. It is based on the expat parser. You can find more details on their webpage if interested. So here is a small sample code that parses an XML file and prints what is parsed into the console. I have put this into the SVN repository (unit tests of the XML module).

Code: Select all

#include <cstdlib>
#include <cstdio>
#include <cstdlib>
#include <iostream>

#include "xml/IReader.h"

class CSampleReaderCallback : public XML::IReaderCallback
{
public:

        CSampleReaderCallback(void)
                :m_ui64Indent(0)
        {
        }

        void indent(void)
        {
                for(XML::uint64 i=0; i<m_ui64Indent; i++)
                {
                        std::cout << "  ";
                }
        }

        virtual void openChild(const char* sName, const char** sAttributeName, const char** sAttributeValue, XML::uint64 ui64AttributeCount)
        {
                this->indent();
                std::cout << "opened node " << sName << (ui64AttributeCount?" with attributes : ":" with no attribute");
                for(XML::uint64 i=0; i<ui64AttributeCount; i++)
                {
                        std::cout << "[" << sAttributeName[i] << "=" << sAttributeValue[i] << "]";
                }
                std::cout << "\n";
                m_ui64Indent++;
        }

        virtual void processChildData(const char* sData)
        {
                this->indent();
                std::cout << "data for this node is [" << sData << "]\n";
        }

        virtual void closeChild(void)
        {
                m_ui64Indent--;
                this->indent();
                std::cout << "closed node\n";
        }

        XML::uint64 m_ui64Indent;
};

int main(int argc, char** argv)
{
        if(argc<2)
        {
                printf("syntax : %s fileout.xml\n", argv[0]);
                return -1;
        }

        CSampleReaderCallback l_oSampleReaderCallback;
        XML::IReader* l_pReader=XML::createReader(l_oSampleReaderCallback);

        FILE* l_pFile=fopen(argv[1], "rb");
        if(l_pFile)
        {
                char l_pBuffer[1024];
                while(!feof(l_pFile))
                {
                        size_t len=fread(l_pBuffer, 1, sizeof(l_pBuffer), l_pFile);
                        l_pReader->processData(l_pBuffer, len);
                }
                fclose(l_pFile);
        }

        l_pReader->release();
        l_pReader=NULL;

        return 0;
}
I hope this helps,
tell me if you need more details.

Yann

jbrumberg
Posts: 5
Joined: Mon Jul 12, 2010 7:02 pm

Re: XML Reader

Post by jbrumberg »

Hi Yann,

Thank you for the XML parser example. I am a researcher at Boston University. We have previously discussed multiple g.USBAmp configurations on the IRC, though I only just registered on the forum. I am considering using an XML configuration file for use with multiple boxes, similar to the electrode localization box. My thinking is to create an XML reader box, which can transmit the contents of a single XML file to two or more analysis/stimulation/visualization boxes.

Thanks,
Jon

Post Reply