main in XMLToCSVBasic.cpp

The structure of the main routine is nearly identical to the Java implementation. This subsection covers a few things that need to be pointed out.

From the Logic for main
Set up DOM XML environment (dependent on implementation)
Load input XML Document (dependent on implementation)

Due to the COM considerations and the need to check for return values rather than relying on exceptions to be thrown, this translates into a bit more code in the C++ implementation than it did in Java.

From XMLToCSVBasic.cpp
//  Set up the COM environment, since MSXML is a
//  COM Component
hResult = CoInitialize(NULL);
if (FAILED(hResult))
{
  cerr << "Failed to initialize COM environment" << endl;
  return 0;
}

//  We'll do our main processing within a try block
//  We only expect to throw COM, MSXML parse
//  and I/O exceptions
try
{
  //  Declare smart pointers for our MSXML COM Interfaces.
  //  We'll use the Document2 class for all our stuff so that
  //  we can do schema validation.
  //  We do these within the try block so that when we exit
  //  the program they will be out of scope.
  IXMLDOMDocument2Ptr spDocInput;
  IXMLDOMNodeListPtr spRowList;

  //  Create the COM DOM Document object
  hResult =
      spDocInput.CreateInstance(__uuidof(DOMDocument40));

  if FAILED(hResult)
  {
    throw cCreateInstanceError;
  }

  //  Tell it we don't want to load the document
  //  asynchronously
  spDocInput->async =  VARIANT_FALSE;

  //  Load input XML Document (dependent on implementation)
  hResult = spDocInput->load(cInputXMLName);

  // Check for errors
  if( hResult != VARIANT_TRUE)
  {
    spParseError = spDocInput->GetparseError();
    cerr << "Parsing Error" << endl;
    displayParseError(spParseError);
    throw cParseError;
  }

We first need to set up our COM environment. Next, we create spDocInput (an IXMLDOMDocument2Ptr) as a COM object. We set its async property to false since we want to wait until it is completely loaded before we proceed. Finally, we load the document using the DOM Level 3 Load semantics.

Again, the rest of the main routine is pretty straightforward. Here's the part that does the most work.

//  Initialize CSVRowWriter object
RowWriter = new CSVRowWriter(&OutputCSV);

//  NodeList of Rows <- Call Document's
//    getElementsByTagName for all elements named Row
spRowList = spDocInput->getElementsByTagName("Row");

//  DO until Rows NodeList.item[index] is null
//    Call CSVRowWriter write method, passing
//      NodeList.item[index]
//    Increment index
//  ENDDO

while (spRowList->item[iRows] != NULL)
{
  IXMLDOMElementPtr spRow = spRowList->item[iRows];
  RowWriter->write(spRow);
  iRows++;
}

Note that spRowList is an IXMLDOMNodeListPtr object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset