1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Rep Power

    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,

  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Rep Power

    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
    will return:
    Username: test
    Password: test
    Root Tag Name: login

IMN logo majestic logo threadwatch logo seochat tools logo