C#中的高级SAX解析器

时间:2009-07-03 11:16:23

标签: .net python xml parsing sax

请参阅下面的XML Arch。 我想在行/列wize中显示它。

我需要的是我需要将此xml文件转换为Hashtable,如

{"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>

1 个答案:

答案 0 :(得分:0)

  

来自xml.sax.handler import   ContentHandler导入xml类   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]或last_dict [x]                   其他:                       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"
     

如果名称 =='主要':       fp = open('Form.xml','r')       xml.sax.parse(fp,my_handler())       fp.close()

     

最后,而不是C#,它已经   使用Pyton Script解决了,我在这里   分享脚本。感谢。