[Tutor] Pretty XML

Ismael Garrido ismaelgf at adinet.com.uy
Wed Nov 23 19:43:17 CET 2005


Greg Lindstrom wrote:

> Hello-
> I am in the process of creating an XML document from information 
> stored in our database.  One of my colleagues will use the record to 
> format our information (health care claims) into all sorts of forms, 
> reports, etc.  He is partial to PHP5 but I like Python and would like 
> to know if there is something that would read in my XML file and 
> format it in a similar manner to "pretty print" so I can verify the 
> correct information is being pulled.  I have looked into the XML 
> documentation and, to be honest, I am overwhelmed with the choices; 
> SAX, DOM, XPath, 4Suite, and more.  Though I've been coding full time 
> for 25 years, I'm new to XML and could use some pointers.
>
> My immediate concern is to read in an XML stream from a file and 
> format it with indentation so that I can read and verify the data 
> against out database.  My long term concern is what tool(s) do you 
> think would give the biggest return against the effort required to 
> learn them?  The claim files I am generating will be less than a meg 
> each, if that matters.
>
> Thanks!
> --greg


Try 4Suite / Amara: 
http://uche.ogbuji.net/uche.ogbuji.net/tech/4suite/amara/ (I downloaded 
the allinone package)

 >>> from Ft.Xml import Parse
 >>> from Ft.Xml.Domlette import PrettyPrint
 >>> xmlfile = open('c:/test.xml')
 >>> xml = "".join(xmlfile.readlines())
 >>> xml
'<xml><level1><level2>some text<level3>level 3</level3></level2>some 
other text</level1><another>Hi!<empty /></another></xml>\n'
 >>> doc = Parse(xml)
 >>> PrettyPrint(doc)
<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <level1>
    <level2>some text<level3>level 3</level3>
    </level2>some other text</level1>
  <another>Hi!<empty/>
  </another>
</xml>

It isn't a great representation... Perhaps my xml was malformed. Try it 
yourself to see if it works for you.

HTH!
Ismael


More information about the Tutor mailing list