在Java中读取XML时出错

时间:2018-05-09 18:02:54

标签: java xml

我需要读取XML文件,当我运行以下错误消息时:

  

线程“main”中的异常java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.DeferredTextImpl无法强制转换为org.w3c.dom.Element
  在leituraxml.LeituraXML.main(LeituraXML.java:40)

守则:

public class LeituraXML {
  public static void main(String[] args) throws SAXException, IOException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();            
        Document doc = builder.parse("C:\\catalogo.xml");

        NodeList catalogo = doc.getElementsByTagName("rec");

        int tamanhoDoCatalogo = catalogo.getLength();

        for (int i  = 0; i < tamanhoDoCatalogo; i++){

            Node noExemplar = catalogo.item(i);
            if (noExemplar.getNodeType()==Node.ELEMENT_NODE){
                Element elementoExemplar = (Element)noExemplar;
                NodeList listaDeExemplares = elementoExemplar.getChildNodes();

                for (int j = 0; j<listaDeExemplares.getLength(); i++){

                    Node noFilho = listaDeExemplares.item(j);
                    if(noFilho.getNodeType()==Node.ELEMENT_NODE);
                    Element elementoFilho = (Element) noFilho;
                    switch(elementoFilho.getTagName()){
                        case "v018":
                            System.out.println(elementoFilho.getTextContent());
                    }
                }
            }
        }

    } catch (ParserConfigurationException ex) {
        Logger.getLogger(LeituraXML.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

XML文件摘要:

<rec>
    <v001>001</v001>
    <v002>201260927231</v002>
    <v003>616.890231</v003>
    <v005>7</v005>
    <v006>m</v006>
    <v007>^a115001^b1^c1^d20100000^e2^fOvernight^m1</v007>
    <v007>^a115002^b2^c1^d20100000^e1^m2</v007>
    <v007>^a115003^b3^c1^d20100000^e1^m3</v007>
    <v007>^a115004^b4^c1^d20100000^e1^m4</v007>
    <v007>^a115005^b5^c1^d20100000^e1^m5</v007>
    <v007>^a115006^b6^c1^d20100000^e1^m6</v007>
    <v008>http://www.conselho.saude.gov.br/14cns/docs/constituicaofederal.pdf</v008>
    <v016>ROCHA, Ruth Mylius</v016>
    <v018>Enfermagem em Saúde Mental</v018>
    <v020>187</v020>
    <v022>9</v022>
    <v040>1</v040>
    <v061>-187iografia: p. 182-187</v061>
    <v062>Ed. SENAC Nacional</v062>
    <v063>2. ed. atual. e ampl</v063>
    <v064>2005</v064>
    <v065>20050000</v065>
    <v066>Rio de Janeiro</v066>
    <v069>8574581658</v069>
    <v070>http://phl.sjdr.ifsudestemg.edu.br:8080/imagens/Enfermagem em saúde mental.jpg</v070>
    <v085>4</v085>
    <v087>Enfermagem psiquiátrica</v087>
    <v087>Saúde mental</v087>
    <v087>Substâncias psicoativas</v087>
    <v087>Psiquiatria</v087>
    <v090>b</v090>
    <v096>http://www.conselho.saude.gov.br/14cns/docs/constituicaofederal.pdf</v096>
    <v103>R672e</v103>
    <v998>1</v998>
    <v999>uper^bsuper^bsuper^bsuper</v999>
    <v999>^d20180412^h103632^bthais</v999>
</rec>

0 个答案:

没有答案