INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Parse XML

Parse XML

(OP)
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.

CODE --> 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



RE: Parse XML

XPath support

CODE --> python

for row in root.findall('.//row[@CUSTOMER_ID]'):
    print(row.attrib.get('CUSTOMER_ID'), row.attrib.get('CUSTOMER_NAME')) 

RE: Parse XML

(OP)
Cool

Thanks
Simi

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close