Java XML SAX Parser extractClasses(String xmlContent)

Here you can find the source of extractClasses(String xmlContent)

Description

extract Classes

License

Open Source License

Declaration

public static Collection<String> extractClasses(String xmlContent) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.util.Collection;

import java.util.LinkedHashSet;

import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Main {
    public static Collection<String> extractClasses(String xmlContent) {
        final Set<String> classList = new LinkedHashSet<String>();
        try {/*from ww  w .java  2  s.com*/
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            DefaultHandler handler = new DefaultHandler() {
                public void startElement(String uri, String localName, String qName, Attributes attributes)
                        throws SAXException {
                    if (attributes != null) {
                        for (int i = 0; i < attributes.getLength(); i++) {
                            if (attributes.getQName(i).equals("class")) {
                                classList.add(attributes.getValue(i));
                            }
                        }
                    }
                }
            };
            byte[] bytes = xmlContent.getBytes("UTF8");
            InputStream inputStream = new ByteArrayInputStream(bytes);
            InputSource source = new InputSource(inputStream);
            saxParser.parse(source, handler);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return classList;
    }
}

Related

  1. createSchema(String url)
  2. createXMLReader()
  3. createXmlReader()
  4. createXmlReader()
  5. createXmlReader()
  6. getFactory()
  7. getSAXParser()
  8. getSAXParser()
  9. getSAXParser()