[ create a new paste ] login | about

Link: http://codepad.org/4FLXi6bA    [ raw code | output | fork ]

Python, pasted on Feb 18:
import xml.etree.ElementTree as et
# Load the xml content from a string


xml_from_above='''<persons>
  <person id="54321">
    <name>
      <first>Troy</first>
      <middle>J</middle>
      <last>Grosfield</last>
    </name>
    <address>
      <street>123 North Lane</street>
      <state>CO</state>
      <city>Denver</city>
      <zip>12345</zip>
    </address>
  </person>
</persons>'''




content = et.fromstring(xml_from_above)

# Get the person or use the .findall method to get all
# people if there's more than person
person = content.find("person")
first_name = person.find("name/first")
middle_name = person.find("name/middle")
last_name = person.find("name/last")

# Get the persons address
address = person.find("address")
street = address.find("street")
state =  address.find("state")
city= address.find("city")
zip = address.find("zip")

# Print output
print "id: " + person.attrib.get('id')
print "first name: " + first_name.text
print "middle name: " + middle_name.text
print "last name: " + last_name.text
print "street: " + street.text
print "state: " + state.text
print "city: " + city.text
print "zip: " + zip.text


Output:
1
2
3
4
5
6
7
8
id: 54321
first name: Troy
middle name: J
last name: Grosfield
street: 123 North Lane
state: CO
city: Denver
zip: 12345


Create a new paste based on this one


Comments: