In the series of basic tutorials I'm going to use the example of cusomer info that you need to import/export in the application. Why? Because there are a lot of formats available for this kind of data and it could get quite cumbersome to handle changes between formats everywhere in your program.

In this tutorial we're just going to import the customer data from a file in our native XML format. The data we have about each customer is very simple. We have the company name, its address and its phone number. The file that we'll save under customers.xml looks like this:

<customers>
	<customer>
		<company>Al Bundy's shoes</company>
		<address>No hope lane 34</address>
		<phone>55528974637</phone>
	</customer>
	<customer>
		<company>Bob's fries</company>
		<address>No name avenue 56</address>
		<phone>555968273328</phone>
	</customer>
</customers>

This is the code we'll use to import our data:

// SDX
#include <SDXDocumentBuilder.h>
#include <SDXXMLFileReader.h>

// Dummy DB funtions
void startRecord();
void writeField(std::string name, std::string value);
void writeField(std::string name, int value);
void endRecord();

using namespace std;

int main(int argc, char** argv){
	/// This class allows you to build DOM-like structures through
	/// the ContentHandler interface.
	SDX::Helpers::DocumentBuilder docBuilder;

	SDX::Formats::XmlFileReader iFile("customers.xml", &docBuilder);

	if(!iFile.read()){
		cerr << "File processing failed: " << iFile.getError() << endl;
		return 1;
	}

	vector<SDX::Node*> customers(docBuilder.getDocument()->getChildren("customer"));
	for(int i = 0; i < customers.size(); ++i){
		startRecord();

		std::string fieldValue;
		if(customers[i]->getNodeAttributeValue(&fieldValue, "company"))
			writeField("company", fieldValue);
		if(customers[i]->getNodeAttributeValue(&fieldValue, "address"))
			writeField("address", fieldValue);

		int phoneNumber;
		if(customers[i]->getNodeAttributeValue(&phoneNumber, "phone"))
			writeField("address", phoneNumber);

		endRecord();
	}

	return 0;
}

In the lines before the main function we include two thing from SDX and declare some dummy functions that write records to our fictional database.

The first class we instantiate in our main function is a DocumentBuilder. This class allows you to build DOM-like structures through the functions startDocument and endDocument, startNode and endNode and writeAttribute reimplemented from the ContentHandler interface. This interface is one of the core elments of SDX, but more about that in the next tutorial.

The second class we instantiate is an XmlFileReader. This class simply reads XML data passes it to the ContentHandler it has been passed, which in this case is the DocumentBuilder instance.

After the instantiation of the two objects we tell the XmlFileReader class to actually read the file and send the data to the passed DocumentBuilder. We print an error and return if there was some sort of error during the proccessing of the file.

Then we loop through all the customers in the document built by the DocumentBuilder. The root node of an XML file is ignored since it's a format-specific thing. For each customer we add a record at the start of the loop and end the record at the end of the loop.

Then we call Node::getNodeAttributeValue to retrieve the values of the child nodes of the current customer node. Now you may say: "I understand that it gets the value of a child node, but what about the Attribute part?". Well, the only thing that SDX knows is nodes, named attributes and unnamed attributes. Nodes are also known as elements in an XML structure and named attributes are the normal attributes you know from XML. SDX uses unnamed attributes to store texts of XML nodes. To test whether an attribute is an unnamed attribute, use Attribute::isNamedAttribute. So the text of the <customer> node is the node's first unnamed attribute.

That concludes the first tutorial. You have learned what a DocumentBuilder is, how SDX's DOM-like structure looks like and how to read an XML file. Now would be a good time to read the API docs of the Node and Attribute classes. Note that the Node class is derived from the NodeContainer class so it can contain child nodes. To read CSV or Tab Based files, check out the corresponding classes in the API docs, they are in the corresponding StreamReader classes. To write files StreamWriter classes are available. For each StreamReader (for example XmlStreamReader), a corresponding FileReader (XmlFileReader) is available, and for each StreamWriter, a corresponding FileWriter (XmlFileWriter) is available (as you hopefully can see, I've tried to make the library as predictable as possible). Also, check out the demos in the Tests/ directory of SDX.