E ai pessoal, tenho um ficheiro XML e quero pegar o seu seu valor. Acredito que estou fazendo isso errado no metodo characters. Alguma idea de como pegar esse valor??
package sax;
import java.util.ArrayList;
import java.util.List;
import model.Contact;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ContactHandler extends DefaultHandler {
private List<Contact> contacts;
private Contact currentContact;
private String content;
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("Inicio do documento");
contacts=new ArrayList<Contact>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if ("contact".equals(localName)) {
currentContact = new Contact();
contacts.add(currentContact);
} else if ("name".equals(localName)) {
currentContact.setName(content);
System.out.println(content);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
content = new String(ch, start, length);
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("Fim doc.");
}
public List<Contact> getContactos() {
return contacts;
}
}