Skip to main content
Use python formatting.
Source Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 121
  • 155

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 
for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

Output:

1 2 

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

Output:

1 2 

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

Output:

1 2 
Include output.
Source Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 121
  • 155

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

And similar, usually pretty simple, code patterns.Output:

1 2 

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

And similar, usually pretty simple, code patterns.

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

Output:

1 2 

I suggest ElementTree. There are other compatible implementations of the same API, such as lxmllxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

And similar, usually pretty simple, code patterns.

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

And similar, usually pretty simple, code patterns.

I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed -- the ease of programming part depends on the API, which ElementTree defines.

First build an Element instance root from the XML, e.g. with the XML function, or by parsing a file with something like:

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

Or any of the many other ways shown at ElementTree. Then do something like:

for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value) 

And similar, usually pretty simple, code patterns.

fixed code not working; improved formatting & readability
Source Link
Loading
Rollback to Revision 6
Source Link
Daniel F
  • 14.3k
  • 15
  • 96
  • 131
Loading
Added information about the C implementation of ElementTree, because I was about to dump the idea of using it (because was unable to load a 300MB XML file in a sensible amount of time) until I used this c-variant.
Source Link
Daniel F
  • 14.3k
  • 15
  • 96
  • 131
Loading
add import
Source Link
Franck Dernoncourt
  • 84.7k
  • 81
  • 374
  • 556
Loading
fixed minor syntax error
Source Link
Martin Thoma
  • 138.6k
  • 174
  • 687
  • 1.1k
Loading
added 320 characters in body
Source Link
Alex Martelli
  • 887.4k
  • 175
  • 1.3k
  • 1.4k
Loading
added 7 characters in body
Source Link
0xC0000022L
  • 21.3k
  • 11
  • 102
  • 165
Loading
Source Link
Alex Martelli
  • 887.4k
  • 175
  • 1.3k
  • 1.4k
Loading