< Reading & Writing XML 4 | Main | Reading & Writing XML 6 >


 

 

Reading and Writing XML 5

 

 

What we have in this page?

 

 

 

  1. Writing XML Using XmlTextWriter...continue

 

 

4.        Add this code to the start of the main() function to check the number of arguments and save the path.

// Check for required arguments

if (args->Length < 2)

{

   Console::WriteLine(L"Usage: CppXmlWriter path");

   return -1;

}

String^ path = gcnew String(args[1]);

 

Adding code to the start of the main() function to check the number of arguments and save the path

 

5.        Create an XmlTextWriter by adding the following code, which is very similar to the code used to create an XmlTextReader.

try

{

   // Create the writer...

   // Use the default encoding, Unicode

   XmlTextWriter^ writer = gcnew XmlTextWriter(path, nullptr);

}

catch (Exception^ pe)

{

   Console::WriteLine(pe->ToString());

}

 

Adding code to create an XmlTextWriter, which is very similar to the code used to create an XmlTextReader

 

The writer is created by specifying the path for the new file and the character encoding that should be used. Passing a null pointer means that the writer will use the default UTF-8 encoding, which is a good default choice. If you want to use another encoding, such as UTF-7 or ASCII, you can specify a System::Text::Encoding object of the appropriate type.

 

6.        Let’s write the XML declaration to the file. Add the following lines to the end of the code inside the try block:

// Set the formatting

writer->Formatting = Formatting::Indented;

 

// Write the standard document start

writer->WriteStartDocument();

        

// Flush and close

writer->Flush();

writer->Close();

 

Adding code to write the XML declaration to the file

 

XmlTextWriter can produce output indented or without formatting. The default is no formatting, so you need to set the Formatting property if you want indentation. The defaults for the indentation character (a space) and the indentation level (two characters) are usually quite acceptable.

WriteStartDocument produces a standard XML declaration. To make sure that all the text is output to the file, you should call Flush and Close before exiting.

 

7.        Next, write the root element to the document, as shown here:

// Start the root element

writer->WriteStartElement(L"geology");

 

// Close the root element

writer->WriteEndElement();

 

 

 

 

Adding code to write the root element to the document

 

The content of the root element will go between the calls to WriteStartElement and WriteEndElement. There isn’t any content in this case, but you still need both calls.

 

8.        We add some more messages to the standard output to make our program more interactive. A complete code for this part is given below.

try

{

      // Create the writer...

      // Use the default encoding, Unicode

      Console::WriteLine("Creating the writer...");

      XmlTextWriter^ writer = gcnew XmlTextWriter(path, nullptr);

 

      // Set the formatting

      Console::WriteLine("Setting the format...");

      writer->Formatting = Formatting::Indented;

 

      // Write the standard document start

      Console::WriteLine("Writing the standard doc format...");

      writer->WriteStartDocument();

 

      // Start the root element

      Console::WriteLine("Starting the root element...");

      writer->WriteStartElement(L"geology");

 

      // Close the root element

      Console::WriteLine("Closing the root element...");

      writer->WriteEndElement();

           

      // Flush and close

      Console::WriteLine("Flushing and closing...");

      writer->Flush();

      writer->Close();

}

9.        Build and run the application at this stage, giving the name of the XML file.

 

C++ .NET XmlWriter program output example

 

10.     Open testxml.xml file. You’ll see that the program writes an empty root element.

<?xml version="1.0"?>

<geology />

 

testxml.xml file content

 

11.     To see how some of the other methods of XmlTextWriter are used, add one of the volcano entries to the root element, as shown here:

try

{

   // Create the writer...

   // Use the default encoding, unicode

   Console::WriteLine("Creating the writer...");

   XmlTextWriter^ writer = gcnew XmlTextWriter(path, nullptr);

 

   // Set the formatting

   Console::WriteLine("Setting the format...");

   writer->Formatting = Formatting::Indented;

 

   // Write the standard document start

   Console::WriteLine("Writing the standard doc format...");

   writer->WriteStartDocument();

 

   // Start the root element

   Console::WriteLine("Starting the root element...");

   writer->WriteStartElement(L"geology");

 

   // Start the volcano element

   Console::WriteLine("Starting the volcano element...");

   writer->WriteStartElement(L"volcano");

 

   // Do the name attribute

   Console::WriteLine("Setting the name attribute...");

   writer->WriteAttributeString(L"name", L"Mount St.Helens");

 

   // Write the location element

   Console::WriteLine("Writing the location element...");

   writer->WriteStartElement(L"location");

   writer->WriteString(L"Washington State, USA");

   writer->WriteEndElement();

 

   // Write the height element

   Console::WriteLine("Writing the height element...");

   writer->WriteStartElement(L"height");

   writer->WriteAttributeString(L"value", "9677");

   writer->WriteAttributeString(L"unit", "ft");

   writer->WriteEndElement();

 

   // Write the type element

   Console::WriteLine("Writing the type element...");

   writer->WriteStartElement(L"type");

   writer->WriteString(L"stratovolcano");

   writer->WriteEndElement();

 

   // Write the eruption elements

   Console::WriteLine("Writing the eruption elements...");

   writer->WriteStartElement(L"eruption");

   writer->WriteString(L"1857");

   writer->WriteEndElement();

   writer->WriteStartElement(L"eruption");

   writer->WriteString(L"1980");

   writer->WriteEndElement();

 

   // Write the magma element

   Console::WriteLine("Writing the magma element...");

   writer->WriteStartElement(L"magma");

   writer->WriteString(L"basalt, andesite and dacite");

   writer->WriteEndElement();

        

   // Close the volcano element

   Console::WriteLine("Closing the volcano element...");

   writer->WriteEndElement();

 

   // Close the root element

   Console::WriteLine("Closing the root element...");

   writer->WriteEndElement();

        

   // Flush and close

   Console::WriteLine("Flushing and closing...");

   writer->Flush();

   writer->Close();

}

We’ve left in the root element code so that you can see how everything nests. Adding extra elements isn’t hard, but it’s rather long-winded, and you have to be careful to nest all the calls correctly.

 

12.     Build and run the program, providing it with a suitable file name. Here, we use testxml.xml as the file name. The file should contain XML that looks very much like this:

 

 

 

 

Creating an XML file, testxml.xml

 

13.     Check the testxml.xml content using any XML editor such as Microsoft FrontPage or text editor such as Notepad or Wordpad.

 

Checking the testxml.xml content using any XML editor

 

You can see how all the elements contain their attributes, how they are nested correctly, and how everything is properly indented.

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

 

 


< Reading & Writing XML 4 | Main | Reading & Writing XML 6 >