piątek, 15 czerwca 2012

Generating and parsing an xml documents

From time to time some projects required to manipulate/generate/parse an XML documents. This kind of work was always pain in the ass for me and I was unwilling to do such job. All modules I used to reach XML related goals wasn't too intuitive. Yes, there is xml.etree which is best module for common tasks but is not to much objective.

Thats why I finally pushed myself to develop my own manipulation tool. I think this module is more object-oriented than xml.etree and developer friendly at the same time. "Easyxml" is currently under construction and contains only basic XML generator:
  • generating XML document,
  • setting basic properties ("version", "encodings").
  • creating XML element,
  • adding attributes,
  • adding sub-elements,
  • generating XML document as a string (indentation and separation can be modified),
  • writing XML document to file, PIPE and STDOUT.
Things that will be done in the future:
  • removing pointed element,
  • searching element with "xpath",
  • removing elements,
  • reorganizing elements order,
  • parsing a XML documents,
  • refactoring.
Here is an example of use:
 from easyxml import XMLDocument, XMLElement  
   
 document = XMLDocument(root='address_book', encoding='UTF8')  
 usr1 = XMLElement(name='user', attributes={'firstname': 'Wiktor', 'lastname': 'Wrutek'})  
 usr1_addr = XMLElement(name='addres')  
 usr1_street = XMLElement(name='street', body='Dubois')  
 usr1_number = XMLElement(name='number', body='48')  
   
 # add street and home number as an sub-element of address  
 usr1_addr.add_element([usr1_street, usr1_number])  
   
 # add address as an sub-element of user  
 usr1.add_element(usr1_addr)  
   
 # append elements to document  
 # currently there is no special method to do this  
 document.elements.append(usr1)  
   
 # indentation set on 4 spaces  
 print document.string(indent='  ')  


Above will produce following output:
   
 <?xml version="0.1" encoding="UTF8"?>  
 <address_book>  
   <user lastname="'Wrutek'" firstname="'Wiktor'">  
     <addres>  
         <street>  
                 Dubois  
         </street>  
         <number>  
                 48  
         </number>  
     </addres>  
   </user>  
 </address_book>  
   


I've got hope that this will be useful not only for me. I'll try to update this project as often as it is possible.