How to export a dataset to an XML workspace document


Summary
This article shows how to use the IGdbXmlExport interface to export schema and data or schema from a geodatabase using the GdbExporter co-class. This example allows you to create a function by passing in the database name and passing out an Extensible Markup Language (XML) file name from an existing geodatabase.

In this topic


Exporting a dataset to an XML workspace document

The steps in this section show how to export a feature dataset from a personal geodatabase.  After opening a workspace, a name object is retrieved for the first feature dataset found.  A scratch workspace is then created and cast to the IName interface for the sake of creating a name mapping (necessary for the export process).  Finally, the IGdbXmlExport interface is used to write the contents of the feature dataset to a new XML document.
The following code opens an existing personal geodatabase:
[Java]
// Open the source geodatabase.
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactory();
IWorkspace workspace = workspaceFactory.openFromFile(databaseName, 0);
The following code retrieves a name object for the first feature dataset in the geodatabase.  If no feature datasets are found, an exception will be thrown.
[Java]
// Retrieve the first feature dataset from the workspace.
IEnumDatasetName enumDatasetName = workspace.getDatasetNames
    (esriDatasetType.esriDTFeatureDataset);
enumDatasetName.reset();
IName featureDatasetName = (IName)enumDatasetName.next();
if (featureDatasetName == null){
    throw new Exception("No feature datasets exist in the specified geodatabase.");
}
A new NamesEnumeratorClass is then created, and the feature dataset name added to it:
[Java]
// Create a new enumerator and add the feature dataset name.
IEnumNameEdit enumNameEdit = new NamesEnumerator();
enumNameEdit.add(featureDatasetName);
IEnumName enumName = (IEnumName)enumNameEdit;
The following code creates a new scratch workspace and casts it to the IName interface to prepare for name mapping:
[Java]
// Create a scratch workspace factory. 
IScratchWorkspaceFactory scratchWorkspaceFactory = new ScratchWorkspaceFactory();
IWorkspace scratchWorkspace = scratchWorkspaceFactory.createNewScratchWorkspace();
IDataset dataset = (IDataset)scratchWorkspace;
IName workspaceName = dataset.getFullName();
The following code will create a GeoDBDataTransfer object and use it to generate a name mapping, which checks for conflicts that will occur during the data transfer.
In the scenario presented by this example, conflicts should not occur, since the mapping is generated between an existing Access geodatabase and a newly-created scratch workspace, which is also base on an Access database.  If the XML document is being created for transfer between different data source types, or to a geodatabase with existing data, conflicts may occur.  For more information on handling conflicts, see the article How to use copy and paste to transfer data.
[Java]
// Create a GeoDBDataTransfer object and create a name mapping.
IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransfer();
IEnumNameMapping[] enumNameMapping = new IEnumNameMapping[1];
boolean hasConflicts = geoDBDataTransfer.generateNameMapping(enumName, workspaceName,
    enumNameMapping);
if (hasConflicts){
    // TODO: Handle this in a way appropriate to your application.
}
The following code exports the dataset schema and data by transferring the dataset(s) in the enumNameMapping enumerator to the output XML file (outputXmlFile), with boolean parameters set so that the geometry is exported in binary format, the XML document is not compressed, and the dataset metadata is included.
The outputXmlFile is a string that identifies the output XML file, where you must specify the file extension. For example, the outXmlFile could have the following extension of foo.xml, foo.ZIP, or foo.Z:
[Java]
// Create an exporter and export the dataset with binary geometry, not compressed,
// and including metadata.
IGdbXmlExport gdbXmlExport = new GdbExporter();
gdbXmlExport.exportDatasets(enumNameMapping[0], outputXmlFile, true, false, true);

Complete code example

The following code is a complete method which performs all of the steps shown in the previous section:
[Java]
static void exportDatasetToXML(String databaseName, String outputXmlFile)throws
    Exception{
    // Open the source geodatabase.
    IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactory();
    IWorkspace workspace = workspaceFactory.openFromFile(databaseName, 0);
    // Retrieve the first feature dataset from the workspace.
    IEnumDatasetName enumDatasetName = workspace.getDatasetNames
        (esriDatasetType.esriDTFeatureDataset);
    enumDatasetName.reset();
    IName featureDatasetName = (IName)enumDatasetName.next();
    if (featureDatasetName == null){
        throw new Exception(
            "No feature datasets exist in the specified geodatabase.");
    }
    // Create a new enumerator and add the feature dataset name.
    IEnumNameEdit enumNameEdit = new NamesEnumerator();
    enumNameEdit.add(featureDatasetName);
    IEnumName enumName = (IEnumName)enumNameEdit;
    // Create a scratch workspace factory. 
    IScratchWorkspaceFactory scratchWorkspaceFactory = new ScratchWorkspaceFactory();
    IWorkspace scratchWorkspace = scratchWorkspaceFactory.createNewScratchWorkspace()
        ;
    IDataset dataset = (IDataset)scratchWorkspace;
    IName workspaceName = dataset.getFullName();
    // Create a GeoDBDataTransfer object and create a name mapping.
    IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransfer();
    IEnumNameMapping[] enumNameMapping = new IEnumNameMapping[1];
    boolean hasConflicts = geoDBDataTransfer.generateNameMapping(enumName,
        workspaceName, enumNameMapping);
    if (hasConflicts){
        // TODO: Handle this in a way appropriate to your application.
    }
    // Create an exporter and export the dataset with binary geometry, not compressed,
    // and including metadata.
    IGdbXmlExport gdbXmlExport = new GdbExporter();
    gdbXmlExport.exportDatasets(enumNameMapping[0], outputXmlFile, true, false, true)
        ;
}






Development licensingDeployment licensing
ArcGIS for Desktop StandardArcGIS for Desktop Standard
ArcGIS for Desktop AdvancedArcGIS for Desktop Advanced
Engine Developer KitEngine: Geodatabase Update