views:

446

answers:

1

See Below is the XML Arch. I want to display it in row / column wize.

What I need is I need to convert this xml file to Hashtable like,

{"form" : {"attrs" : { "string" : " Partners" }
          {"child1": { "group" : { "attrs" : { "col" : "6", "colspan":"1" } }
                                 { "child1": { "field" : { "attrs" : { "name":"name"} 
                                 { "child2": { "field" : { "attrs" : { "name":"ref"} }
          {"child2": { "notebook" : "attrs" : {"colspan": 4} } } 
}

<?xml version="1.0" encoding="utf-8"?>
<form string="Partners">
                    <group col="6" colspan="4">
                        <field name="name" select="1"/>
                        <field name="ref" select="1"/>
                        <field name="customer" select="1"/>
                        <field domain="[('domain', '=', 'partner')]" name="title"/>
                        <field name="lang" select="2"/>
                        <field name="supplier" select="2"/>
                    </group>
                    <notebook colspan="4">
                        <page string="General">
                            <field colspan="4" mode="form,tree" name="address" nolabel="1" select="1">


                            </field>
                            <separator colspan="4" string="Categories"/>
                            <field colspan="4" name="category_id" nolabel="1" select="2"/>
                        </page>
                        <page string="Sales &amp; Purchases">
                            <separator colspan="4" string="General Information"/>
                            <field name="user_id" select="2"/>
                            <field name="active" select="2"/>
                            <field name="website" widget="url"/>
                            <field name="date" select="2"/>
                            <field name="parent_id"/>
                            <newline/>
                        <newline/><group col="2" colspan="2" name="sale_list">
                        <separator colspan="2" string="Sales Properties"/>
                        <field name="property_product_pricelist"/>
                    </group><group col="2" colspan="2">
                        <separator colspan="2" string="Purchases Properties"/>
                        <field name="property_product_pricelist_purchase"/>
                    </group><group col="2" colspan="2">
<separator colspan="2" string="Stock Properties"/>
<field name="property_stock_customer"/>
<field name="property_stock_supplier"/>
</group></page>
                        <page string="History">
                            <field colspan="4" name="events" nolabel="1" widget="one2many_list"/>
                        </page>
                        <page string="Notes">
                            <field colspan="4" name="comment" nolabel="1"/>
                        </page>
                    <page position="inside" string="Accounting">
                    <group col="2" colspan="2">
                        <separator colspan="2" string="Customer Accounting Properties"/>
                        <field name="property_account_receivable"/>
                        <field name="property_account_position"/><field name="vat" on_change="vat_change(vat)" select="2"/><field name="vat_subjected"/>
                        <field name="property_payment_term"/>
                    </group>
                    <group col="2" colspan="2">
                        <separator colspan="2" string="Supplier Accounting Properties"/>
                        <field name="property_account_payable"/>
                    </group>
                    <group col="2" colspan="2">
                        <separator colspan="2" string="Customer Credit"/>
                        <field name="credit" select="2"/>
                        <field name="credit_limit" select="2"/>
                    </group>
                    <group col="2" colspan="2">
                        <separator colspan="2" string="Supplier Debit"/>
                        <field name="debit" select="2"/>
                    </group>
                    <field colspan="4" context="address=address" name="bank_ids" nolabel="1" select="2">


                    </field>
                </page>
               </notebook>
</form>
A: 

from xml.sax.handler import ContentHandler import xml class my_handler(ContentHandler):

def get_attr_dict(self, attrs):
    ret_dict = {}
    for name in attrs.getNames():
        ret_dict[name] = attrs.getValue(name)
    #end for name in attrs.getNames():
    return ret_dict

def setDocumentLocator(self, locator):
    print "DOCUMEN T LOOCATOR"
    pass

def startDocument(self):
    self.my_data = {}
    self.my_stack = []
    print "SATART DUASDFASD:"

def startElement(self, name, attrs):
    attr_dict = self.get_attr_dict(attrs)
    myname = name!='field' and name or attr_dict['name']
    append_dict = {
                    'attrs' : attr_dict,
                    'childs' : []
                }

    if not self.my_data:
        self.my_data[name] = append_dict
    else:
        last_dict = {}
        for x in self.my_stack:
            if last_dict:
                last_dict = isinstance(last_dict, list) and

last_dict[-1][x] or last_dict[x] else: last_dict = self.my_data[x] last_dict.append({myname : append_dict})

    self.my_stack.extend([myname, 'childs'])

def endElement(self, name):
    self.my_stack = self.my_stack[:-2]
    print "ENDS ELERMERE :",name

def endDocument(self):
    print "Sfled :",self.my_data
    print "ENDA DAFASDFASD"

if name == 'main': fp = open('Form.xml', 'r') xml.sax.parse(fp, my_handler()) fp.close()

Finally instead of C#, it has been solved using Pyton Script, here I'm sharing the script. Thanks.