Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse XML

Status
Not open for further replies.

simian336

Programmer
Sep 16, 2009
723
US
Hey, I am having problems figuring out how to parse an xml file... It seems to be abnormal as compared to all the examples I find.

Here is my data...

<?xml version="1.9"?>
<doc>
<md_cst.get_CL status="9">
<RS1>
<row ROW_NUM="1" CUSTOMER_ID="199147" CUSTOMER_NAME="DOE JOHN A " STATUS="A" ZIPCODE="27929" REGION_ID="1" ECI="199147" LR="N" EC="Y" HP="Y" HV="N" />
<row ROW_NUM="2" CUSTOMER_ID="199652" CUSTOMER_NAME="DOE JOHN B " STATUS="A" ZIPCODE="27929" REGION_ID="1" ECI="199652" LR="N" EC="Y" HP="Y" HV="N" />
<row ROW_NUM="3" CUSTOMER_ID="199653" CUSTOMER_NAME="DOE JOHN C " STATUS="A" ZIPCODE="27996" REGION_ID="1" ECI="199653" LR="N" EC="Y" HP="Y" HV="N" />
<row ROW_NUM="4" CUSTOMER_ID="199654" CUSTOMER_NAME="DOE JOHN D " STATUS="A" ZIPCODE="27996" REGION_ID="1" ECI="199654" LR="N" EC="Y" HP="N" HV="N" />
<row ROW_NUM="5" CUSTOMER_ID="199656" CUSTOMER_NAME="DOE JOHN E " STATUS="A" ZIPCODE="27929" REGION_ID="1" ECI="199656" LR="N" EC="Y" HP="Y" HV="N" />
</RS1>
<RS2>
<row PAGES="1" COUNTS="3948" />
</RS2>
</md_cst.get_CL>
</doc>

********************************************************************
I have pieced together this code which basically prints everything.

Python:
from xml.etree import ElementTree as et

filename='_FullCustList.txt'

tree=et.parse(filename)

root=tree.getroot()
print(root)

print(root.tag)
print(root.attrib)

for child in root:
    print(child.tag, child.attrib)
    for c in child:
        print(c.tag, c.attrib)
        for a in c:
            print(a.tag, a.attrib)


But I need to be able to parse particular elements like CUSTOMER_ID etc.

Thanks

Simi



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top