The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > org.xml.sax  [3 examples]

e549. Getting the Attributes of an Element During XML SAX Parsing

    // Create a handler for SAX events
    DefaultHandler handler = new MyHandler();
    
    // Parse an XML file using SAX;
    // e517 The Quintessential Program to Parse an XML File Using SAX
    parseXmlFile("infilename.xml", handler, true);
    
    // This class listens for startElement SAX events
    static class MyHandler extends DefaultHandler {
        // This method is called when an element is encountered
        public void startElement(String namespaceURI, String localName,
                                 String qName, Attributes atts)  {
            // Get the number of attribute
            int length = atts.getLength();
    
            // Process each attribute
            for (int i=0; i<length; i++) {
                // Get names and values for each attribute
                String name = atts.getQName(i);
                String value = atts.getValue(i);
    
                // The following methods are valid only if the parser is namespace-aware
    
                // The uri of the attribute's namespace
                String nsUri = atts.getURI(i);
    
                // This is the name without the prefix
                String lName = atts.getLocalName(i);
            }
        }
    }

 Related Examples
e548. Intercepting All Accesses to External Entities During XML SAX Parsing
e550. Determining the Parsing Location of an XML SAX Parser


© 2002 Addison-Wesley.