June 11th, 2004, 08:17 AM
Python and XML -- getting element list
I'm trying to find a way of getting a list of all elements and attributes allowed in an XML DTD (so that I can then compare this against an XML instance and see which of the lements/attributes are not being used). Is there a way to do this with Python and SAX? I'm a comparative newbie to Python, so would be very grateful if someone could explain how I can do this with a small code example.
Thank you in advance,
June 21st, 2004, 11:47 PM
check this out
This is the module you'll want to use. It will parse the XML just like you want. SAX is used for event based XML parsing, which means that certain functions will be called based on the contents of the XML being parsed. It did not sound like this is the kind of functionality you wanted.
Here's the example you wanted. This example parses a simple string. There are more examples in the link provided above, including the ability to parse an XML file.
dom = parseString(""<login password="test" username="test" />")
print "Username:", dom.documentElement.getAttribute("username")
print "Password:", dom.documentElement.getAttribute("password")
print "Root Tag Name:", dom.documentElement.tagName
Root Tag Name: login