I would like to parse xml in python, but as a string, not taken from a file. Can someone help me do this?
Asked
Active
Viewed 1.3k times
4 Answers
13
From a file, you could normally do it as
from xml.dom import minidom
xmldoc = minidom.parse('~/diveintopython/common/py/kgp/binary.xml')
For a string, you can change it to
from xml.dom import minidom
xmldoc = minidom.parseString( Your string goes here )

Ken Wayne VanderLinde
- 18,915
- 3
- 47
- 72
3
You could use: xml.dom.minidom.parseString(text)
This method creates a StringIO object for the string and passes that on to parse().
You could also use the same technique of using StringIO for any other XML parser that expects a file-like object.
import StringIO
your_favourite_xml_parser.parse(StringIO.StringIO('<xml>...</xml>'))

Mark Byers
- 811,555
- 193
- 1,581
- 1,452
2
You can use (xml.etree.cElementTree) also.
import xml.etree.cElementTree as ET
aElement = ET.fromstring('<Root id="UUID_1"><Item id="id_Item" /></Root>')
See Python help document
Each element has a number of properties associated with it:
a tag which is a string identifying what kind of data this element represents (the element type, in other words).
a number of attributes, stored in a Python dictionary.
a text string.
an optional tail string.
a number of child elements, stored in a Python sequence

Yao yu
- 21
- 1
2
You can also use lxml. My startup (http://dealites.com) involves a lot of XML processing everyday. I have tried almost every xml library available in python. lxml is the best library available for xml processing.
You can also try Beautiful soup. It is great for HTML parsing but a good alternative to lxml.
lxml example:
from lxml import etree;
parsedfeed = etree.xml('your xml here');
Beautiful Soup example:
from BeautifulSoup import BeautifulStoneSoup;
soup = BeautifulStoneSoup('your xml here');

Javier C.
- 7,859
- 5
- 41
- 53

Rahul Arora
- 1,036
- 9
- 8