Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bssys.azkserver.lucene;
- import java.io.File;
- import java.util.ArrayList;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- public class GetElementAttributesInSAXXMLParsing extends DefaultHandler {
- private ArrayList<String> al = new ArrayList<>();
- public static void main(String[] args) throws Exception {
- DefaultHandler handler = new GetElementAttributesInSAXXMLParsing();
- SAXParserFactory factory = SAXParserFactory.newInstance();
- factory.setValidating(false);
- SAXParser parser = factory.newSAXParser();
- parser.parse(new File("in.xml"), handler);
- }
- @Override
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- // get the number of attributes in the list
- int length = attributes.getLength();
- // process each attribute
- for (int i = 0; i < length; i++) {
- String value = attributes.getValue(i);
- if (value != null && !value.equals("")) {
- al.add(value);
- }
- }
- }
- public String getValues() {
- StringBuilder sb = new StringBuilder();
- for (String s : al) {
- sb.append(s);
- sb.append(" ");
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement