e549. getting the attributes of an element during xml sax parsing.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 32 行

TXT
32
字号
 // 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);
            }
        }
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?