Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package grondz;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintStream;
- import javax.swing.tree.DefaultMutableTreeNode;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.parsers.SAXParser;
- import javax.xml.parsers.SAXParserFactory;
- import org.xml.sax.Attributes;
- import org.xml.sax.ErrorHandler;
- import org.xml.sax.SAXException;
- import org.xml.sax.SAXParseException;
- import org.xml.sax.XMLReader;
- import org.xml.sax.helpers.DefaultHandler;
- /**
- *
- * @author grondz
- */
- public class SaxParser extends DefaultHandler {
- private StringBuffer znaky;
- private DefaultMutableTreeNode root;
- private DefaultMutableTreeNode currentRoot;
- private String fileURI;
- public SaxParser(String aFileURI, DefaultMutableTreeNode anode) {
- super();
- fileURI = aFileURI;
- root = anode;
- }
- @Override
- public void startDocument () throws SAXException
- {
- System.out.println("Document start....");
- znaky = new StringBuffer(500);
- currentRoot = root;
- }
- @Override
- public void endDocument () throws SAXException
- {
- System.out.println("Document stop....");
- }
- @Override
- public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException
- {
- System.out.println("Start element. uri:" + uri + ". local name:" +localName + ". qName:" + qName);
- StringBuffer sb = new StringBuffer();
- sb.append(qName);
- for(int i = 0; i < attributes.getLength(); i++){
- //System.out.println(attributes.getValue(i));
- sb.append(" " + attributes.getQName(i) + "=" +
- attributes.getValue(i));
- }
- DefaultMutableTreeNode anode = new DefaultMutableTreeNode(sb.toString());
- currentRoot.add(anode);
- currentRoot = anode;
- }
- @Override
- public void endElement (String uri, String localName, String qName) throws SAXException
- {
- String content = znaky.toString().trim();
- DefaultMutableTreeNode anode;
- System.out.println("End element. uri:" + uri + ". local name:" +localName + ". qName:" + qName);
- System.out.println("Znaky:" + content);
- if (!content.isEmpty()) {
- anode = new DefaultMutableTreeNode(znaky.toString().trim());
- currentRoot.add(anode);
- }
- anode = new DefaultMutableTreeNode("/" + qName);
- currentRoot.add(anode);
- currentRoot = (DefaultMutableTreeNode)currentRoot.getParent();
- znaky.delete(0, znaky.length());
- }
- @Override
- public void characters (char ch[], int start, int length) throws SAXException
- {
- //System.out.print("Znaky:");
- //System.out.print(new String(ch));
- //System.out.println(".Koniec znakov");
- znaky.append(ch, start, length);
- }
- class MyErrorHandler implements ErrorHandler {
- private PrintStream out;
- MyErrorHandler(PrintStream out) {
- this.out = out;
- }
- private String getParseExceptionInfo(SAXParseException spe) {
- String systemId = spe.getSystemId();
- if (systemId == null) {
- systemId = "null";
- }
- String info = "URI=" + systemId + " Line="
- + spe.getLineNumber() + ": " + spe.getMessage();
- return info;
- }
- public void warning(SAXParseException spe) throws SAXException {
- out.println("Warning: " + getParseExceptionInfo(spe));
- }
- public void error(SAXParseException spe) throws SAXException {
- String message = "Error: " + getParseExceptionInfo(spe);
- throw new SAXException(message);
- }
- public void fatalError(SAXParseException spe) throws SAXException {
- String message = "Fatal Error: " + getParseExceptionInfo(spe);
- throw new SAXException(message);
- }
- }
- private static String convertToFileURL(String filename) {
- String path = new File(filename).getAbsolutePath();
- if (File.separatorChar != '/') {
- path = path.replace(File.separatorChar, '/');
- }
- if (!path.startsWith("/")) {
- path = "/" + path;
- }
- return "file:" + path;
- }
- public void parseXML() throws ParserConfigurationException, SAXException, IOException {
- SAXParserFactory spf = SAXParserFactory.newInstance();
- //spf.setNamespaceAware(true);
- SAXParser saxParser = spf.newSAXParser();
- XMLReader xmlReader = saxParser.getXMLReader();
- xmlReader.setContentHandler(this);
- xmlReader.setErrorHandler(new SaxParser.MyErrorHandler(System.err));
- xmlReader.parse(fileURI);
- //System.out.println("Koniec parsovania....");
- }
- public static void main(String[] args) throws Exception {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement