SCA

SDO XML Helper API examples

test_sdoXMLLoad.c
Loading XML from a buffer
test_sdoXMLLoad_invalidxml.c
Loading invalid XML
test_sdoXMLLoad_empty.c
Loading invalid empty XML
test_fdoXMLSave.c
Saving XML to a file
test_fdoXMLLoad.c
Loading XML from a file; note this reads-in file created in previous test

test_sdoXMLLoad.c

#include "SDO.h"
#include "assert.h"


// Loading XML from a buffer
void test_sdoXMLLoad() {
	SDOFACTORY df = getDataFactory();
	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "tmp");
	sdoXMLLoad(doc, "<test a=\"hello\"/>");
	char buffer[100];
	sdoSave(doc, buffer, 0, 0);
	assert(!strcmp("<test a=\"hello\"/>", buffer));
}

test_sdoXMLLoad_invalidxml.c

#include "SDO.h"
#include "assert.h"


// Loading invalid XML
void test_sdoXMLLoad_invalidxml() {
	SDOFACTORY df = getDataFactory();
	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "tmp");
	assert(!sdoXMLLoad(doc, "<test a=\"hello\">"));
}

test_sdoXMLLoad_empty.c

#include "SDO.h"
#include "assert.h"


// Loading invalid empty XML
void test_sdoXMLLoad_empty() {
	SDOFACTORY df = getDataFactory();
	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "tmp");
	assert(!sdoXMLLoad(doc, ""));
}

test_fdoXMLSave.c

#include "SDO.h"
#include "assert.h"


// Saving XML to a file
void test_fdoXMLSave() {
	SDOFACTORY df = getDataFactory();
	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "tmp");
	sdoXMLLoad(doc, "<test a=\"hello\"/>");
	FILE *f = fopen("test_fdoXMLSave.tmp", "w");
	fdoXMLSave(xmlhlp, doc, f);
	fclose(f);
	f = fopen("test_fdoXMLSave.tmp", "r");
	char buffer[100];
	fgets(buffer, (int) sizeof(buffer), f); 
	fclose(f);
	assert(!strcmp("<test a=\"hello\"/>", buffer));
}

test_fdoXMLLoad.c

#include "SDO.h"
#include "assert.h"


// Loading XML from a file;
// note this reads-in file created in previous test
void test_fdoXMLLoad() {
	SDOFACTORY df = getDataFactory();
	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "tmp");
	FILE *f = fopen("test_fdoXMLSave.tmp", "r");
	fdoXMLLoad(xmlhlp, doc, f);
	fclose(f);
	char buffer[100];
	sdoSave(getXMLRootDataObject(doc), buffer, 0, 0);
	assert(!strcmp("<test a=\"hello\"/>", buffer));
}