Leitura do arquivo XML

boa noite galera …

sou novo no fórum a gostaria de uma ajuda

tenho meu código que uso o JAX-B para leitura de um arquivo XML que esta em um diretório especifico mas quando chega em determinada linha do código ele pula direta mento para a linha de erro :

a baixo esta o código que estou usando

public static void main(String[] args) throws JAXBException {

    String arquivoXML;
    arquivoXML = "c:\\xml\\teste.xml";

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Object.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        
        File lerNfe = new File(arquivoXML);
      [b]  ObjectFactory nfe = (ObjectFactory) jaxbUnmarshaller.unmarshal(lerNfe);[/b]

        System.out.println("lendo o arquivo XML" + nfe);

    } catch (Exception ex) {
        System.out.println("Error ao ler XML " + ex.getMessage());
    }

}

quando ele chega nessa linha
ObjectFactory nfe = (ObjectFactory) jaxbUnmarshaller.unmarshal(lerNfe);
ele pula para o erro alguém poderia me ajudar

desde já agradeço pela atenção

o erro que aprece e esse :

org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.

welinton19, abra o xml que está tentando converter no IE e veja se consegue ler.

parece que tem algum alguma tag com problema, ou conteúdo inválido.

bom dia igrsantos obrigado pela atenção …

assim cara já tinha visto isso ontem anoite e mudei o XML

ai o erro mudou agora e esse:

Error ao ler XML javax.xml.bind.UnmarshalException: unexpected element (uri:“http://www.portalfiscal.inf.br/nfe”, local:“nfeProc”). Expected elements are (none)

ainda não sei o motivo …

desde já agradeço pela atenção …

welinton19, tem como postar o mapeamento da tua classe ?

dá uma olhada se não está faltando essa annotation:

caro igrsantos

a minha classe e e basicamente isso que postei a cima.

essa

public static void main(String[] args) throws JAXBException {

    String arquivoXML;
    arquivoXML = "C:/xml/53130211082913000110550010000000031000068023.xml";

    try {
        File lerNfe = new File(arquivoXML);
        JAXBContext jaxbContext = JAXBContext.newInstance(Object.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        ObjectFactory nfe = (ObjectFactory) jaxbUnmarshaller.unmarshal(lerNfe);

        System.out.println("lendo o arquivo XML" + nfe);

    } catch (Exception ex) {
        System.out.println("Error ao ler XML " + ex);
    }
}

a aonde poderia coloca essa conotação no meu código ?

esse é o problema,

dá uma lida nesse link que achei navegando na net aqui.

http://javac.com.br/jc/posts/list/92.page

Acho que vai te ajudar !

caro igrsantos valeu pela ajuda

boa tarde galera depois de lutar com a leitura do XML conseguir não só ler o xml mais também pegar os dados

postarei o codigo aqui espero que possa ajudar valeu pala ajuda …

public static void main(String argv[]) {

    try {

        File fXmlFile = new File("C:/xml/53130211082913000110550010000000031000068023.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Elemento raiz :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("nfeProc");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                System.out.println("infNFe versao Id Name : " + eElement.getAttribute("versao"));
                System.out.println("chNFe Name : " + eElement.getElementsByTagName("chNFe").item(0).getTextContent());
                System.out.println("cUF Name : " + eElement.getElementsByTagName("cUF").item(0).getTextContent());
                System.out.println("cNF Name : " + eElement.getElementsByTagName("cNF").item(0).getTextContent());
                System.out.println("dhRecbto Name : " + eElement.getElementsByTagName("dhRecbto").item(0).getTextContent());
               System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());;
                
                

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

abraço

muito bom welinton19, marcar o tópico como resolvido :wink:

igrsantos esta como favorito obrigado pela ajuda …