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

    Join Date
    Jul 2003
    Posts
    30
    Rep Power
    11

    Python and XML -- getting element list


    Hi,
    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,

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

    Join Date
    Jun 2004
    Posts
    7
    Rep Power
    0

    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.

    http://www.python.org/doc/2.3.4/lib/...m.minidom.html

    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.

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

IMN logo majestic logo threadwatch logo seochat tools logo