0

I have a XML file which looks like:

<root>
  <product>
  ...multiple tags
  </product>
  <product>
  ...multiple tags
  </product>
  .
  .
  .
</root>

There are multiple products in the file, each having a set of tags. I want to pass the XML corresponding to a product as an argument for a HTTP request. I looked through this but could not find how to "get" a child element.

Could someone please help. Thanks

EDIT: I have tried using:

import xml.etree.ElementTree as ET
tree = ET.parse('sample.xml')
root = tree.getroot()

for child in root:
    print child       //also tried child.text

But I get the following output and not the XML corresponding to each child:

<Element 'product' at 0xb729328c>
<Element 'product' at 0xb7293d0c>
<Element 'product' at 0xb72987ec>
<Element 'product' at 0xb729b2cc>
<Element 'product' at 0xb729bcec>
5
  • What do you mean by "XML request"? Commented Dec 17, 2013 at 13:07
  • HTTp request actually, giving the xml as a parameter Commented Dec 17, 2013 at 13:08
  • You seem a bit confused. You do have the XML corresponding to each child, in the Element objects. You've just printed them, rather than actually dumping their text content with ET.dumps(child), but you don't actually want to pass that dump to the function, you want the child element you already have, which you can manipulate. Commented Dec 17, 2013 at 13:24
  • 1
    use tostring: for child in root: x = ET.tostring(child); print x Commented Dec 17, 2013 at 13:49
  • @CorleyBrigman I think this should work. Thanks a lot.!! Commented Dec 17, 2013 at 13:54

2 Answers 2

1

You usually do something like:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
root.findall('product')

The result from root.findall will return all the product items (as an array), so you can do:

for product in root.findall('product'):

Would go through all the child items

Sign up to request clarification or add additional context in comments.

6 Comments

I can use for child in root as well. But that gives me some sort of code, print child = <Element 'product' at 0xb729328c>. I want the XML under product. Any way of doing that?
You want the XML or the object?
How about using itertext
I want the XML under each child
@MaxSpencer : I've looked through this link, but it doesn't mention how I can get the XML corresponding to a child element.
|
0

As I understand it you have an XML file and you want to extract the data for each <product /> element, as an XML string which you can use in a HTTP request. By extending what @nrathaus has already said I hope to give a slightly more complete answer to your question.

We can get a list of Element objects, corresponding to the <product /> elements as follows:

from xml.etree import ElementTree

tree = ElementTree.parse('products.xml')
root = tree.getroot()
product_elements = root.findall('product')

Then to turn each of these elements into an XML string use ElementTree.tostring. For example:

for product_element in product_elements:
    print(ElementTree.tostring(product_element))

Example

products.xml

<products>
  <product>
    <name>First product</name>
  </product>
  <product>
    <name>Second product</name>
  </product>
  <product>
    <name>Third product</name>
  </product>
</products>

test.py

from xml.etree import ElementTree

tree = ElementTree.parse('products.xml')
root = tree.getroot()
product_elements = root.findall('product')

for product_element in product_elements:
    print(ElementTree.tostring(product_element))

Output

/tmp/xml$ python --version
Python 2.7.3
/tmp/xml$ python test.py 
<product>
    <name>First product</name>
  </product>

<product>
    <name>Second product</name>
  </product>

<product>
    <name>Third product</name>
  </product>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.