//[c] //[of]: initializeAsmXml int initializeAsmXml(const char* schemaFilename) { // Initialize the AsmXml library ax_initialize((void*)malloc, (void*)free); // Initialize the class context int res = ax_initializeClassParser(&classContext); if( res != 0 ) return 1; // Read the schema and compile it char* buf = readFile(schemaFilename); if( !buf ) return 1; docClass = ax_classFromString(buf, &classContext); free(buf); if( docClass == NULL ) return 1; return 0; }
//[cf] //[of]:main int main(int argc, char *argv[]) { if( argc != 2 ) { puts("usage: db2html <docbook_file>"); return 1; } ax_initialize((void*)malloc, (void*)free); AXClassContext classContext; int res = ax_initializeClassParser(&classContext); if( res != 0 ) return 1; AXElementClass* docbookClass = readClass(schemaFilename, &classContext); if( docbookClass == NULL ) return 1; AXParseContext parseContext; res = ax_initializeParser(&parseContext, chunkSize); if( res != 0 ) return 1; AXElement* book = readDocument(argv[1], &parseContext, docbookClass); if( book == NULL ) { printAsmXmlError(&parseContext); return 1; } DocbookToHTML db; db.processBook(book); puts(db.toString()); // Release the document and its class ax_releaseParser(&parseContext); ax_releaseClassParser(&classContext); return 0; }
/////////////////////////////////////////////////////////////////////////////// // main /////////////////////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { int res; AXClassContext classContext; AXParseContext parseContext; AXElementClass* friendClass; AXElement* friends; AXElement* friend; // Initialize the AsmXml library // // Pass the malloc() and free() functions // ax_initialize(malloc, free); // Initialize the class context // // It can store one or more classes. Classes read with this // context are kept in memory as long as it is not released. // res = ax_initializeClassParser(&classContext); // An error while initialization means that allocation failed. // It should never happen since it allocates only 4K. if( res != 0 ) return 1; // Read the schema and compile it // friendClass = readClass(schemaFilename, &classContext); if( friendClass == NULL ) return 1; // Initialize the parser // // Documents read with this parser will stay in memory as long as // the parser is not released. // // The choice of the chunk size is very important since the // performance can be affected by this value. The parser allocates // memory by chunks to reduce calls to malloc that can be very slow. // The ideal value is around 50% of the source XML to process. // res = ax_initializeParser(&parseContext, chunkSize); // An error while initialization means that initial allocation failed. if( res != 0 ) return 1; // Read the file and parse it // friends = readDocument(xmlFilename, &parseContext, friendClass); if( friends == NULL ) { printAsmXmlError(&parseContext); return 1; } // Enumerate child elements friend = friends->firstChild; while( friend ) { printf("================================\n"); printf("Friend ID: %s\n", asString(&friend->attributes[0])); printf("Name: %s\n", asString(&friend->attributes[1])); printf("UserID: %s\n", asString(&friend->attributes[2])); friend = friend->nextSibling; printf("================================\n"); } // Release the document and its class ax_releaseParser(&parseContext); ax_releaseClassParser(&classContext); return 0; }