4 - Arquivo OlaMundo.xml
Suponha o seguinte arquivo xml mnimo:
<?xml version="1.0" encoding="UTF-8"?>
<recado>
<de>Akira</de>
<para>Todos</para>
<mensagem>Ola Mundo!</mensagem>
</recado>
java.io.FileReader;
org.xml.sax.Attributes;
org.xml.sax.InputSource;
org.xml.sax.XMLReader;
org.xml.sax.helpers.DefaultHandler;
org.xml.sax.helpers.XMLReaderFactory;
7 - Mtodos callback
Alm de analisar cada tag, interessante tambm obter os dados - os
contedos - contidos em cada tag. Para isso, seria interessante
realizar uma consulta a API. Observe abaixo os mtodos callback - so
aqueles que recebem notificaes quando algum evento ocorre. No
Descrio
Receive notification of
character data inside an
element.
void endDocument()
void error(SAXParseException e)
Receive notification of a
recoverable parser error.
Receive notification of
ignorable whitespace in
element content.
Receive notification of a
notation declaration.
Receive notification of a
skipped entity.
void startDocument()
Receive notification of an
unparsed entity
declaration.
void warning(SAXParseException e)
Receive notification of a
parser warning.
8 - Obtendo dados
Ento, para que possamos analisar os dados podemos inserir o seguinte
mtodo de callback:
...
public void characters (char ch[], int inicio, int comprimento)
{
System.out.print("Caracteres:
\"");
for (int i = start; i < inicio + comprimento; 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");
}
...
java.awt.TextField;
java.io.File;
java.io.FileReader;
javax.swing.JFrame;
javax.swing.JLabel;
javax.swing.JTextField;
org.xml.sax.Attributes;
org.xml.sax.InputSource;
org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class AnalisaForm extends DefaultHandler {
// os mtodos startDocument, endDocument,
// startElement e endElement sobrepem os
// mtodos de mesmas assinaturas
// herdados de DefaultHandler:
JFrame form;
String componente;
public void startDocument() {
System.out.println("Incio do documento");
form = new JFrame();
form.setSize(500, 500);
form.setLayout(null);
}
public void endDocument() {
System.out.println("Final do documento");
form.setVisible(true);
}
public void startElement(String ns, String nome,
String qNome, Attributes atributos) {
if (ns.equals(""))
componente = qNome;
else
componente = nome;
System.out.println("componente="+componente);
if (componente.equals("rotulo")) {
JLabel l = new JLabel();
int x = Integer.parseInt(atributos.getValue("","x"));
int y = Integer.parseInt(atributos.getValue("", "y"));
String valor = atributos.getValue("", "valor");
l.setText(valor);
form.add(l);
l.setBounds(x,y,45,15);
} else if (componente.equals("campotexto")) {
JTextField t = new JTextField();
int x = Integer.parseInt(atributos.getValue("", "x"));
int y = Integer.parseInt(atributos.getValue("", "y"));
int tamanho = Integer.parseInt(atributos.getValue("",
"tamanho"));
String valor = atributos.getValue("", "valor");
t.setText(valor);
t.setSize(tamanho, 20);
form.add(t);
t.setLocation(x,y);
}
}
public void endElement(String ns, String nome, String qNome) {
}
public void characters(char ch[], int inicio, int comprimento) {
System.out.print("Caracteres:
\"");
for (int i = inicio; i < inicio + comprimento; 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");
http://www.saxproject.org/quickstart.html
Processing XML with Java de Elliotte Rusty Harold:
http://www.cafeconleche.org/books/xmljava/chapters/index.html
http://www.cafeconleche.org/books/xmljava/chapters/ch06.html
Parseando um XML com o SAX por Guilherme Silveira:
http://www.guj.com.br/java.tutorial.artigo.15.1.guj