Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. package com.bssys.azkserver.lucene;
  2.  
  3.  
  4. import java.io.File;
  5. import java.util.ArrayList;
  6.  
  7. import javax.xml.parsers.SAXParser;
  8. import javax.xml.parsers.SAXParserFactory;
  9.  
  10. import org.xml.sax.Attributes;
  11. import org.xml.sax.SAXException;
  12. import org.xml.sax.helpers.DefaultHandler;
  13.  
  14. public class GetElementAttributesInSAXXMLParsing extends DefaultHandler {
  15. private ArrayList<String> al = new ArrayList<>();
  16.  
  17. public static void main(String[] args) throws Exception {
  18. DefaultHandler handler = new GetElementAttributesInSAXXMLParsing();
  19. SAXParserFactory factory = SAXParserFactory.newInstance();
  20. factory.setValidating(false);
  21. SAXParser parser = factory.newSAXParser();
  22. parser.parse(new File("in.xml"), handler);
  23. }
  24.  
  25. @Override
  26. public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  27. // get the number of attributes in the list
  28. int length = attributes.getLength();
  29.  
  30. // process each attribute
  31. for (int i = 0; i < length; i++) {
  32. String value = attributes.getValue(i);
  33. if (value != null && !value.equals("")) {
  34. al.add(value);
  35. }
  36. }
  37. }
  38.  
  39. public String getValues() {
  40. StringBuilder sb = new StringBuilder();
  41. for (String s : al) {
  42. sb.append(s);
  43. sb.append(" ");
  44. }
  45. return sb.toString();
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement