Reader Level:
ARTICLE

How to Generate an XML Document Programmatically-Using SAX Technology: Part II

Posted by Bechir Bejaoui Articles | XML June 23, 2008
In the previous article, we’ve proceed to generate an XML document using DOM technology which is document object model, Now, we’ll do the same thing but, this time using SAX technology.
  • 0
  • 0
  • 23143

In the previous article, we've proceed to generate an xml document using DOM technology which is document object model, Now, we'll do the same thing but, this once, using SAX technology.  In fact the SAX or Serial Access parser for Xml is an API dedicated to whom want to manipulate xml files, it's provides mechanism of data reading from  and data writing in xml documents, but you tell me OK, so what is the difference? Or what's new? The DOM is also an API for manipulating XML documents. I tell you it's a legitimate question but this is out of the scope of this tutorial. Never the less; I can say that there is a difference in nature between the two concepts. And I can resume the difference in the following schedule.

DOM SAX
There are formal specifications for the DOM There aren't formal specifications within SAX
The entire xml tree must be in memory before the parser begins to parse It is no compulsory for the entire xml tree to be loaded before the parser begins to parse, it can work with the maximum depth of the given xml tree
Streamed reading from the disk is impossible Streamed reading from the disk is possible
Less problems during the XML validation More problems during the XML validation

If you want to dig into details concerning this issue, I advice you to read this article, browse to the following URL mentioned as follow   http://developerlife.com/tutorials/?p=28.

Now in order to use SAX, we need to define and instantiate an XmlTextWriter object. The last one can have three constructors overload techniques which ill enumerate them as below:

Constructor  Description
XmlTextWriter (TextWriter) Creates an instance of the XmlTextWriter class using the specified TextWriter.
XmlTextWriter (Stream, Encoding) Creates an instance of the XmlTextWriter class using the specified stream and encoding.
XmlTextWriter (String, Encoding)  Creates an instance of the XmlTextWriter class using the specified file and encoding.

In addition to those representations, I may add one XmlTextWriter (Console.Out) which enables  to display the xml content into  the console. So let's begin with this first one then try to enumerate the rest of techniques.

First way:

Create a new console application then copy and paste the code below:

static void Main(string[] args)

        {

            /*Instantiate an xml text writer and precise that the out put will be

            directed to the console*/

            XmlTextWriter writer = new XmlTextWriter(Console.Out);

            //This define if it is a stand alone xml file or not

            writer.WriteStartDocument(true);

            //This is a comment

            writer.WriteComment("This xml file representation is don with using SAX");

            //Start Familie element which is the parent element

            writer.WriteStartElement("Familie");

           

            //Father

            //Start by by create the element

            writer.WriteStartElement("Father");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My father");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(60);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Mother

            //Start by by create the element

            writer.WriteStartElement("Mother");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My mother");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(55);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Sister

            //Start by by create the element

            writer.WriteStartElement("Sister");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My sister");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(20);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Brother

            //Start by by create the element

            writer.WriteStartElement("Brother");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My borther");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(17);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Mark the end of the main element which is the familie element

            writer.WriteEndElement();

            //Mark the end of the document

            writer.WriteEndDocument();

            //Purge the memory

            writer.Flush();

            //Close the writer

            writer.Close();

            Console.Read();        

        }

Execute the code and observe. The following console will appear as below: 


 
Figure 1

Second way:

In this case an xml file will be generated in the defined emplacement. Replace the previous code with this one:

static void Main(string[] args)

        {

            /*Instantiate an xml text writer and precise the xml file path

            And the encoding type*/

      XmlTextWriter writer = new XmlTextWriter(@"C:\FamilyFile.xml", Encoding.ASCII);

            //This define if it is a stand alone xml file or not

            writer.WriteStartDocument(true);

            //This is a comment

            writer.WriteComment("This xml file representation is don with using SAX");

            //Start Familie element which is the parent element

            writer.WriteStartElement("Familie");

           

            //Father

            //Start by by create the element

            writer.WriteStartElement("Father");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My father");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(60);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Mother

            //Start by by create the element

            writer.WriteStartElement("Mother");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My mother");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(55);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Sister

            //Start by by create the element

            writer.WriteStartElement("Sister");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My sister");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(20);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

            //Brother

            //Start by by create the element

            writer.WriteStartElement("Brother");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My borther");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(17);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Mark the end of the main element which is the familie element

            writer.WriteEndElement();

            //Mark the end of the document

            writer.WriteEndDocument();

            //Purge the memory

            writer.Flush();

            //Close the writer

            writer.Close();

            Console.WriteLine("The xml document is generated successfully!!!")

            Console.Read();        

        }

Now execute the code then browse to C:\FamilyFile.xml open it and observe.

Third way:

It's similar to the previous way except that in this time we will use a stream  instead of the file path

static void Main(string[] args)

        {

            //File stream instantiation

            FileStream oStream = new FileStream(@"C:\FamilyFile1.xml", FileMode.Create, FileAccess.Write);

            /*Instantiate an xml text writer and prcise that the out put will be

            directed to the console*/

            XmlTextWriter writer = new XmlTextWriter(oStream, Encoding.ASCII);

            //This define if it is a stand alone xml file or not

            writer.WriteStartDocument(true);

            //This is a comment

            writer.WriteComment("This xml file representation is don with using SAX");

            //Start Familie element which is the parent element

            writer.WriteStartElement("Familie");

           

            //Father

            //Start by by create the element

            writer.WriteStartElement("Father");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My father");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(60);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Mother

            //Start by by create the element

            writer.WriteStartElement("Mother");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My mother");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(55);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Sister

            //Start by by create the element

            writer.WriteStartElement("Sister");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My sister");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(20);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

            //Brother

            //Start by by create the element

            writer.WriteStartElement("Brother");

            //Add the name attribute

            writer.WriteStartAttribute("Name");

            //Add the name value

            writer.WriteValue("My borther");

            //Mark the name attribute end

            writer.WriteEndAttribute();

            //Add the age attribute

            writer.WriteStartAttribute("Age");

            //Add the age value

            writer.WriteValue(17);

            //Mark the age attribute end

            writer.WriteEndAttribute();

            //Mark the end of the element

            writer.WriteEndElement();

 

            //Mark the end of the main element which is the familie element

            writer.WriteEndElement();

            //Mark the end of the document

            writer.WriteEndDocument();

            //Purge the memory

            writer.Flush();

            //Close the writer

            writer.Close();

            Console.WriteLine("The xml document is generated successfully!!!");

            Console.Read();        

        }

Now, browse to C:\FamilyFile1.xml then open it and observe.

Forth way:

static void Main(string[] args)

        {

            //Instanciate a string builder then add the xml content to it

            StringBuilder oStringBuilder = new StringBuilder();

            oStringBuilder.Append("<?xml version='1.0' encoding='us-ascii' standalone='yes'?>");

            oStringBuilder.Append("<!--This xml file representation is don with using SAX-->");

            oStringBuilder.Append("<Familie>");

            oStringBuilder.Append("<Father Name='My father' Age='60' />");

            oStringBuilder.Append("<Mother Name='My mother' Age='55' />");

            oStringBuilder.Append("<Sister Name='My sister' Age='20' />");

            oStringBuilder.Append("<Brother Name='My borther' Age='17' />");

            oStringBuilder.Append("</Familie>");

 

            //Add a new string writer and overload it by the above string builder instance

            TextWriter oTextStringWriter = new StringWriter(oStringBuilder);

            //Now overload the xmltext writer by the text writer instance

            XmlTextWriter writer = new XmlTextWriter(oTextStringWriter);

            Console.WriteLine("This xmltextwriter is ready to be used");

           

            Console.Read();        

        }

This is the forth and the last way to generate an xml entity using SAX technology.

Good dotneting!!!

COMMENT USING

Trending up