xml

In [ ]:
# config.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<Market name="market1">
		<Dim_Type>product</Dim_Type>
		<Dim_Generic>product_l1</Dim_Generic>
		<Dim_Column1>product_l1_id</Dim_Column1>
		<Dim_Column2>product_l1_desc</Dim_Column2>
		<Dim_Table>dim1</Dim_Table>
	</Market>
	<Market name="market1">
		<Dim_Type>product</Dim_Type>
		<Dim_Generic>product_l2</Dim_Generic>
		<Dim_Column1>product_l2_id</Dim_Column1>
		<Dim_Column2>product_l2_desc</Dim_Column2>
		<Dim_Table>dim1</Dim_Table>
	</Market>
	<Market name="market1">
		<Dim_Type>store</Dim_Type>
		<Dim_Generic>store_size</Dim_Generic>
		<Dim_Column1>size_code</Dim_Column1>
		<Dim_Table>dim2</Dim_Table>
	</Market>
	<Market name="market1">
		<Dim_Type>store</Dim_Type>
		<Dim_Generic>store_region</Dim_Generic>
		<Dim_Column1>region_code</Dim_Column1>
		<Dim_Table>dim2</Dim_Table>
	</Market>
</config>
In [3]:
import xml.dom.minidom
xmlDoc = xml.dom.minidom.parse('config.xml')
print(xmlDoc)
<xml.dom.minidom.Document object at 0x0000000004952948>
In [4]:
def get_config(l_dimension):
    l_market = xmlDoc.getElementsByTagName('Market')
    for m in l_market:
        if m.getAttribute('name') == 'market1':
            l_generic = m.getElementsByTagName('Dim_Generic')[0].childNodes[0].data
            if l_generic == l_dimension.strip():
                l_column1 = m.getElementsByTagName('Dim_Column1')[0].childNodes[0].data
                l_table = m.getElementsByTagName('Dim_Table')[0].childNodes[0].data
                if l_dimension.upper().find('PRODUCT') != -1:
                    l_column2 = m.getElementsByTagName('Dim_Column2')[0].childNodes[0].data
                    return (l_column1, l_column2, l_table)
                else:
                    return (l_column1, l_table)
In [6]:
t_prod1, t_prod2, t_table = get_config('product_l1')
print(t_prod1, t_prod2, t_table)
product_l1_id product_l1_desc dim1
In [8]:
l_dimension = 'product_l1,product_l2,store_size'
list1 = []
t_dim = l_dimension.split(',')
for p in t_dim:
    if p.lower().find('product') != -1:
        t_product1, t_product2, t_table = get_config(p)
        list1.append(t_product1+','+t_product2+','+t_table)
        
print(list1)
['product_l1_id,product_l1_desc,dim1', 'product_l2_id,product_l2_desc,dim1']
www.000webhost.com