SDO API
The following SDO examples are based on the po.xml document used in the SDO tutorial.
SDO API for Opening a Document and Writing It to Standard Out
This SDO API example has the following steps:
- Obtain an instance of a Data Access Service (DAS). DASs are the bridge between the DataObject SDO API used to manipulate all data and the specific data format and storage medium of the data source.
- Use the DAS load() method to read in the XML document.
- Use the DAS save() method to write the document to a data stream. In this case, standard out.
Java SDO API Example
| XMLDataAccessService das = XMLDataAccessServiceFactory.create(); |
//2 |
| DataGraph dataGraph = das.load( new File("po.xml") ); |
//3 |
| DataObject root = dataGraph.getRootObject(); |
//4 |
| DataObject purchaseOrder = root.getDataObject("purchaseOrder"); |
//5 |
| das.save(dataGraph, System.out, null); |
|
C++ SDO API Example
| DataFactoryPtr factory = DataFactory::getDataFactory(); |
//1 |
| XMLDataAccessService das(factory); |
//2 |
| DataGraphPtr dataGraph = das.loadFile("../po.xml"); |
//3 |
| DataObjectPtr root = dataGraph->getRootObject(); |
//4 |
| DataObjectPtr purchaseOrder = root->getDataObject("purchaseOrder"); |
//5 |
| das.save(dataGraph, std::cout); |
|
The notes on the right of the SDO API example are explained below:
- Create a DataFactory instance. This step applies only to C++.
- Create an XmlDataAccessService instance.
- Load the file po.xml into a DataGraph. Notice that the Java method takes a File object, which is created inline.
- Get the DataGraph's root DataObject. This step is not needed to meet the minimum requirements of this task, but it is required before you can manipulate the data in any way.
- Get the top-level element purchaseOrder. Note that C++ returns pointers to objects, and therefore uses the "->" dereferencing notation. This step, too, is not required for this task, but is also a very common operation.
- Save the DataGraph, using the save() method overload that writes to a data stream.
You can see from the SDO API example that:
- The Java and C++ SDO API interfaces are similar, and will be even more so once we start working with DataObject.
- The path to the file to be opened is relative to where the executable is run. For C++, if the executable is created in a build directory such as 12d, the path to the file may need to go up a level to the main project directory, as shown in the example.
- The null parameter in the DAS save() method for Java is for passing options. This has not been implemented, hence the null.
SDO API Example: Open and Write Out customers.xml
You can obtain this SDO API example in the tutorials included with HydraSDO™.
<installdir> utorials\samples
\cpp
hydrasdo.cpp
customers.xml
d
makefile
\java
hydrasdo.java
customers.xml
As with any code, there are various ways you could write SDO examples.
Java SDO API Example
XMLDataAccessService das = XMLDataAccessServiceFactory.create();
DataGraph dataGraph = das.load( new File("customers.xml") );
das.save(dataGraph, System.out, null);
C++ SDO API Example
DataFactoryPtr factory = DataFactory::getDataFactory();
XMLDataAccessService das(factory);
DataGraphPtr dataGraph = das.loadFile("../customers.xml");
das.save(dataGraph, std::cout);