SCA

SDO Sequence API examples

test_sequence_traversal.c
Iterating over items of a sequence

test_sequence_traversal.c

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


// Iterating over items of a sequence
void test_sequence_traversal() {
	SDOFACTORY df = dfAlloc("test");

	// load XML data to traverse into pdo
	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "tmp");
	sdoXMLLoad(doc,
		"<elmt xmlns=\"http://www.example.com\">1<subelmt>2</subelmt>3</elmt>");
	DATAOBJECT pdo = getXMLRootDataObject(doc);

 	int sz = sequenceSize(pdo);
	assert(sz == 3);
	for (int i = 0; i < sz; i++) {
		if (isText(pdo, i)) {
			char *txt = getText(pdo, i);
			// expecting 1 and/or 3 here
			assert(!strcmp("1", txt) || !strcmp("3", txt));
		}
		else {
			DATAOBJECT subpdo = getDataObjectSequenceItemValue(pdo, i);
			char *content = getCStringSequenceItemValue(pdo, i);
			// expecting 2 here
			assert(!strcmp("2", content));
		}
	}
}