Example Code - Python+PythonNet, Ironpython, Boo

srijit at yahoo.com srijit at yahoo.com
Fri Oct 15 09:16:36 CEST 2004


Hello,
Here is the code with modified comments.

Best 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 Python with PythonNet");

#~ 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 School attribute..
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();     

#~ Write the name of the city.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();
  
#~ Write number of students.
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 school 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 Ironpython");

#~ 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 School attribute.
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();     

#~ Write the name of the city.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();
  
#~ Write number of students.
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 school 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 School attribute.
prefix = writer.LookupPrefix("urn:schoolnames");
writer.WriteStartAttribute(prefix, "NAME", "urn:schoolnames");
writer.WriteString("ABCD International School");
writer.WriteEndAttribute();     

// Write the name of the city.
writer.WriteStartElement("city");
writer.WriteString("Madurai");
writer.WriteEndElement();
  
// Write number of students.
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 school 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);


srijit at yahoo.com wrote in message news:<221d8dbe.0410011952.1a45ef12 at posting.google.com>...
> Hello,
> There are mistakes in the comments. I shall correct 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