Aktive lesere: 6

Lokale meninger, globale ytringer

Get attributes of element during SAX parsing

This example show you how to get the attributes of elements in an XML file using the SAX parser.

package org.brudvik.example.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.InputStream;

public class SAXElementAttribute {
    public static void main(String[] args) {
        SAXElementAttribute demo = new SAXElementAttribute();
        demo.run();
    }

    private void run() {
        try {
            //
            // Create SAXParserFactory instance and a SAXParser
            //
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();

            //
            // Get an InputStream to the elements.xml file and parse
            // its contents using the SAXHandler.
            //
            InputStream is =
                    getClass().getResourceAsStream("/elements.xml");
            DefaultHandler handler = new SAXHandler();
            parser.parse(is, handler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class SAXHandler extends DefaultHandler {
        @Override
        public void startElement(String uri, String localName,
                                 String qName, Attributes attributes)
                throws SAXException {

            int attributeLength = attributes.getLength();
            if ("person".equals(qName)) {
                for (int i = 0; i < attributeLength; i++) {
                    //
                    // Get attribute names and values
                    //
                    String attrName = attributes.getQName(i);
                    String attrVal = attributes.getValue(i);
                    System.out.print(attrName + " = " + attrVal + "; ");
                }
                System.out.println("");
            }
        }
    }
}

The elements.xml file is as follow:



[/java]

Hopefully this will make your day a bit easier.

Anbefalte lenker
Aktivitet