Example Code - Python+PythonNet, Ironpython, Boo

Srijit Kumar Bhadra srijitb at gmail.com
Fri Oct 1 23:43:59 EDT 2004


Hello,
There are mistakes in the comments. I shall update it.

With regards,
Srijit

srijit at yahoo.com wrote in message news:<221d8dbe.0409302041.3798265d at posting.google.com>...
> Hello,
> Here is an example code of xml writer in Python+PythonNet, Ironpython
> and Boo. The codes look very similar.
> 
> Regards,
> Srijit
> 
> Python + PythonNet:
> 
> import CLR
> import CLR.System
> from CLR.System import Console as CLR_Console
> import CLR.System.Xml as CLR_Xml
> 
> true = 1
> false = 0
> filename = "test1.xml"
> writer = CLR_Xml.XmlTextWriter(filename, None);
> #~ Use indenting for readability.
> writer.Formatting = CLR_Xml.Formatting.Indented;
> 
> writer.WriteComment("XML in Boo Language");
> 
> #~ Write an element (this one is the root).
> writer.WriteStartElement("EducationalCentres");
> 
> #~ Write the namespace declaration.
> writer.WriteAttributeString("xmlns", "schname", None,
> "urn:schoolnames");
> 
> writer.WriteStartElement("School");
> 
> #~ Lookup the prefix and then write the ISBN attribute.
> prefix = writer.LookupPrefix("urn:schoolnames");
> writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
> writer.WriteString("ABCD International School");
> writer.WriteEndAttribute();     
> 
> #~ Write the title.
> writer.WriteStartElement("city");
> writer.WriteString("Madurai");
> writer.WriteEndElement();
>   
> #~ Write the price.
> writer.WriteElementString("Students", "500");
> 
> #~ Write the style element.
> writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
> writer.WriteString("IGCSE");
> writer.WriteEndElement();
> 
> #~ Write the end tag for the book element.
> writer.WriteEndElement();
> 
> #~ Write the close tag for the root element.
> writer.WriteEndElement();
>  
> #~ Write the XML to file and close the writer.
> writer.Flush();
> writer.Close();
> 
> #~ Read the file back in and parse to ensure well formed XML.
> doc = CLR_Xml.XmlDocument();
> #~ Preserve white space for readability.
> doc.PreserveWhitespace = true;
> #~ Load the file
> doc.Load(filename);
> 
> #~ Write the XML content to the console.
> CLR_Console.Write(doc.InnerXml);
> 
> 
> Ironpython:
> 
> from System import *
> from System.Xml import *
> 
> true = 1
> false = 0
> filename = "test1.xml"
> writer = XmlTextWriter(filename, None);
> #~ Use indenting for readability.
> writer.Formatting = Formatting.Indented;
> 
> writer.WriteComment("XML in Boo Language");
> 
> #~ Write an element (this one is the root).
> writer.WriteStartElement("EducationalCentres");
> 
> #~ Write the namespace declaration.
> writer.WriteAttributeString("xmlns", "schname", None,
> "urn:schoolnames");
> 
> writer.WriteStartElement("School");
> 
> #~ Lookup the prefix and then write the ISBN attribute.
> prefix = writer.LookupPrefix("urn:schoolnames");
> writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
> writer.WriteString("ABCD International School");
> writer.WriteEndAttribute();     
> 
> #~ Write the title.
> writer.WriteStartElement("city");
> writer.WriteString("Madurai");
> writer.WriteEndElement();
>   
> #~ Write the price.
> writer.WriteElementString("Students", "500");
> 
> #~ Write the style element.
> writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
> writer.WriteString("IGCSE");
> writer.WriteEndElement();
> 
> #~ Write the end tag for the book element.
> writer.WriteEndElement();
> 
> #~ Write the close tag for the root element.
> writer.WriteEndElement();
>  
> #~ Write the XML to file and close the writer.
> writer.Flush();
> writer.Close();
> 
> #~ Read the file back in and parse to ensure well formed XML.
> doc = XmlDocument();
> #~ Preserve white space for readability.
> doc.PreserveWhitespace = true;
> #~ Load the file
> doc.Load(filename);
> 
> #~ Write the XML content to the console.
> Console.Write(doc.InnerXml);
> 
> 
> Boo:
> 
> import System
> import System.Xml from System.Xml
> 
> filename = "test1.xml"
> writer = XmlTextWriter(filename, null);
> #~ Use indenting for readability.
> writer.Formatting = Formatting.Indented;
> 
> writer.WriteComment("XML in Boo Language");
> 
> #~ Write an element (this one is the root).
> writer.WriteStartElement("EducationalCentres");
> 
> #~ Write the namespace declaration.
> writer.WriteAttributeString("xmlns", "schname", null,
> "urn:schoolnames");
> 
> writer.WriteStartElement("School");
> 
> #~ Lookup the prefix and then write the ISBN attribute.
> prefix = writer.LookupPrefix("urn:schoolnames");
> writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
> writer.WriteString("ABCD International School");
> writer.WriteEndAttribute();     
> 
> #~ Write the title.
> writer.WriteStartElement("city");
> writer.WriteString("Madurai");
> writer.WriteEndElement();
>   
> #~ Write the price.
> writer.WriteElementString("Students", "500");
> 
> #~ Write the style element.
> writer.WriteStartElement(prefix, "Syllabus", "urn:schoolnames");
> writer.WriteString("IGCSE");
> writer.WriteEndElement();
> 
> #~ Write the end tag for the book element.
> writer.WriteEndElement();
> 
> #~ Write the close tag for the root element.
> writer.WriteEndElement();
>  
> #~ Write the XML to file and close the writer.
> writer.Flush();
> writer.Close();
> 
> #~ Read the file back in and parse to ensure well formed XML.
> doc = XmlDocument();
> #~ Preserve white space for readability.
> doc.PreserveWhitespace = true;
> #~ Load the file
> doc.Load(filename);
> 
> #~ Write the XML content to the console.
> Console.Write(doc.InnerXml);



More information about the Python-list mailing list