Skip to content

Instantly share code, notes, and snippets.

@tuxcuiabano
Created October 9, 2018 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxcuiabano/4cc1074ca39faafb7ca0cd76e6ed7c40 to your computer and use it in GitHub Desktop.
Save tuxcuiabano/4cc1074ca39faafb7ca0cd76e6ed7c40 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package saxhandlerxml;
/**
*
* @author tuxcuiabano Retirado do Livro Texto "Tecnologia de Dados para
* Internet - MACIEL, C, 2018"
*/
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
public class SAXHandlerXML implements ContentHandler {
public SAXHandlerXML() {
super();
}
public void startDocument() throws SAXException {
System.out.println("=== Inicio do documento XML ===");
}
public void endDocument() throws SAXException {
System.out.println("=== Fim do documento XML ===");
}
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (uri.equals("")) {
System.out.println("Start element: " + qName);
} else {
System.out.println("Start element: " + uri + ":" + localName);
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (uri.equals("")) {
System.out.println("End element: " + qName);
} else {
System.out.println("End element: " + uri + ":" + localName);
}
}
public void characters(char ch[], int start, int length) throws SAXException {
System.out.print("Characters: \"");
for (int i = start; i < start + length; i++) {
switch (ch[i]) {
case '\\':
System.out.print("\\\\");
break;
case '"':
System.out.print("\\\"");
break;
case '\n':
System.out.print("\\n");
break;
case '\r':
System.out.print("\\r");
break;
case '\t':
System.out.print("\\t");
break;
default:
System.out.print(ch[i]);
break;
}
}
System.out.print("\"\n");
}
// Esses métodos não fazem nada, mas precisam ser declarados, pois
// estão definidos na interface ContentHandler. Existe um meio alternativo, explicado em seguida (DefaultHandler)
public void processingInstruction(String target, String data) throws SAXException {
}
public void setDocumentLocator(Locator locator) {
}
public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
}
public void skippedEntity(String name) throws SAXException {
}
public void startPrefixMapping(String prefix, String uri) throws SAXException {
}
public void endPrefixMapping(String prefix) throws SAXException {
}
public static void main(String args[]) {
if (args.length == 0) {
System.err.println("Informar o arquivo XML: /" + "java SAXHandlerXML <arquivo.xml>");
System.exit(1);
}
SAXHandlerXML handler = new SAXHandlerXML();
try {
XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(handler);
parser.parse(args[0]);
} catch (SAXException e) {
System.out.println(args[0] + " não está bem formado.");
} catch (IOException e) {
System.out.println("Problema ao carregar o arquivo " + args[0]);
}
} //Fim da função Main
} // Fim da classe XML_SAX_handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment