Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package grondz;
  7.  
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.PrintStream;
  11. import javax.swing.tree.DefaultMutableTreeNode;
  12. import javax.xml.parsers.ParserConfigurationException;
  13. import javax.xml.parsers.SAXParser;
  14. import javax.xml.parsers.SAXParserFactory;
  15. import org.xml.sax.Attributes;
  16. import org.xml.sax.ErrorHandler;
  17. import org.xml.sax.SAXException;
  18. import org.xml.sax.SAXParseException;
  19. import org.xml.sax.XMLReader;
  20. import org.xml.sax.helpers.DefaultHandler;
  21.  
  22. /**
  23. *
  24. * @author grondz
  25. */
  26. public class SaxParser extends DefaultHandler {
  27.  
  28. private StringBuffer znaky;
  29. private DefaultMutableTreeNode root;
  30. private DefaultMutableTreeNode currentRoot;
  31. private String fileURI;
  32.  
  33. public SaxParser(String aFileURI, DefaultMutableTreeNode anode) {
  34. super();
  35. fileURI = aFileURI;
  36. root = anode;
  37. }
  38.  
  39. @Override
  40. public void startDocument () throws SAXException
  41. {
  42. System.out.println("Document start....");
  43. znaky = new StringBuffer(500);
  44. currentRoot = root;
  45. }
  46.  
  47. @Override
  48. public void endDocument () throws SAXException
  49. {
  50. System.out.println("Document stop....");
  51. }
  52.  
  53. @Override
  54. public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException
  55. {
  56. System.out.println("Start element. uri:" + uri + ". local name:" +localName + ". qName:" + qName);
  57. StringBuffer sb = new StringBuffer();
  58. sb.append(qName);
  59. for(int i = 0; i < attributes.getLength(); i++){
  60. //System.out.println(attributes.getValue(i));
  61. sb.append(" " + attributes.getQName(i) + "=" +
  62. attributes.getValue(i));
  63. }
  64. DefaultMutableTreeNode anode = new DefaultMutableTreeNode(sb.toString());
  65. currentRoot.add(anode);
  66. currentRoot = anode;
  67. }
  68.  
  69. @Override
  70. public void endElement (String uri, String localName, String qName) throws SAXException
  71. {
  72. String content = znaky.toString().trim();
  73. DefaultMutableTreeNode anode;
  74.  
  75. System.out.println("End element. uri:" + uri + ". local name:" +localName + ". qName:" + qName);
  76. System.out.println("Znaky:" + content);
  77. if (!content.isEmpty()) {
  78. anode = new DefaultMutableTreeNode(znaky.toString().trim());
  79. currentRoot.add(anode);
  80. }
  81. anode = new DefaultMutableTreeNode("/" + qName);
  82. currentRoot.add(anode);
  83.  
  84. currentRoot = (DefaultMutableTreeNode)currentRoot.getParent();
  85.  
  86. znaky.delete(0, znaky.length());
  87. }
  88.  
  89. @Override
  90. public void characters (char ch[], int start, int length) throws SAXException
  91. {
  92. //System.out.print("Znaky:");
  93. //System.out.print(new String(ch));
  94. //System.out.println(".Koniec znakov");
  95. znaky.append(ch, start, length);
  96. }
  97.  
  98. class MyErrorHandler implements ErrorHandler {
  99. private PrintStream out;
  100.  
  101. MyErrorHandler(PrintStream out) {
  102. this.out = out;
  103. }
  104.  
  105. private String getParseExceptionInfo(SAXParseException spe) {
  106. String systemId = spe.getSystemId();
  107.  
  108. if (systemId == null) {
  109. systemId = "null";
  110. }
  111.  
  112. String info = "URI=" + systemId + " Line="
  113. + spe.getLineNumber() + ": " + spe.getMessage();
  114.  
  115. return info;
  116. }
  117.  
  118. public void warning(SAXParseException spe) throws SAXException {
  119. out.println("Warning: " + getParseExceptionInfo(spe));
  120. }
  121.  
  122. public void error(SAXParseException spe) throws SAXException {
  123. String message = "Error: " + getParseExceptionInfo(spe);
  124. throw new SAXException(message);
  125. }
  126.  
  127. public void fatalError(SAXParseException spe) throws SAXException {
  128. String message = "Fatal Error: " + getParseExceptionInfo(spe);
  129. throw new SAXException(message);
  130. }
  131. }
  132.  
  133. private static String convertToFileURL(String filename) {
  134. String path = new File(filename).getAbsolutePath();
  135. if (File.separatorChar != '/') {
  136. path = path.replace(File.separatorChar, '/');
  137. }
  138.  
  139. if (!path.startsWith("/")) {
  140. path = "/" + path;
  141. }
  142. return "file:" + path;
  143. }
  144.  
  145. public void parseXML() throws ParserConfigurationException, SAXException, IOException {
  146. SAXParserFactory spf = SAXParserFactory.newInstance();
  147. //spf.setNamespaceAware(true);
  148. SAXParser saxParser = spf.newSAXParser();
  149. XMLReader xmlReader = saxParser.getXMLReader();
  150. xmlReader.setContentHandler(this);
  151. xmlReader.setErrorHandler(new SaxParser.MyErrorHandler(System.err));
  152. xmlReader.parse(fileURI);
  153. //System.out.println("Koniec parsovania....");
  154. }
  155. public static void main(String[] args) throws Exception {
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement