Sax - não esta pegando a Tag Inteira

2 respostas
B

Seguinte… preciso de um programa que leia um xml e separa as tags na variaveis, a duvida é que na tag
de alguns xmls de nota fiscal eletronica ele nao me tras tudo oq esta dentro da tag… apenas um pedaço… o que pode ser?
grato,

public class SAXLister {

    public String cnpj;
    public String chave1;
    public String nome1;
    public String nome2;
    public String nomes;
    public String numeroNF;
    public String tudo1;
    int cont = 1;

    public static void main(String[] args) throws Exception {
        new SAXLister("W:/TI/Bruno/Projetos JAVA/XML/4.xml");
    }

    public SAXLister(String caminho) throws SAXException, IOException {
        XMLReader parser = XMLReaderFactory.createXMLReader();
        // should load properties rather than hardcoding class name
        parser.setContentHandler(new PeopleHandler());
        parser.parse(caminho);
    }

    public class PeopleHandler extends DefaultHandler {

        boolean emit = false;
        boolean chave = false;
        boolean nome = false;
        boolean numero = false;
        boolean tudo = false;

        public void startElement(String nsURI, String localName,
                String rawName, Attributes attributes) throws SAXException {

            if (rawName.equalsIgnoreCase("emit")) {
                emit = true;
            }
            if (rawName.equalsIgnoreCase("chNFe")) {
                chave = true;
            }
            if (rawName.equalsIgnoreCase("cNF")) {
                numero = true;
            }
            if (cont == 1) {
                if (rawName.equalsIgnoreCase("xnome")) {
                    nome = true;
                }
            }
        }

        public void characters(char[] ch, int start, int length) {
            if (emit) {
                cnpj = new String(ch, start, length);
                emit = false;
            }
            if (chave) {
                chave1 = new String(ch, start, length);
                chave = false;
            }
            if (numero) {
                numeroNF = new String(ch, start, length);
                numero = false;
            }
            if (nome) {
                nome1 = new String(ch, start, length);
                System.out.println((new String(ch, start, length)));
                nome = false;
                cont = 2;
            }
        }
    }

2 Respostas

denisspitfire

cara deixa eu te fazer uma pergunta… voce consegue jogar os dados lidos ( qualquer dado ) para dentro de um objeto que o unico atributo é uma lista?

B

tipo…
nesse codigo

if (rawName.equalsIgnoreCase("chNFe")) {  
                chave = true;

ele pega as tags certo?
porem alguns xmls de nota fiscal eletronica ele nao pega o numero inteiro…pega apenas metade…

editei o xml
e coloquei uma tag

chNFe1 e coloco o mesmo numero do chNFe e mostrou certinho
agora quando é o chNFe nao mostra…

uma solução e eu pegar o começo do xml
mais como nao esta dentro de uma tag eu nao sei pegar esse valor…

Criado 27 de abril de 2012
Ultima resposta 27 de abr. de 2012
Respostas 2
Participantes 2