0

I am trying to get the content of an xml node, but this is what I get as output :

Exception in thread "main" java.lang.AbstractMethodError: 
org.apache.xerces.dom.DeferredElementImpl.getTextContent()Ljava/lang/String;  
at cities2.main(cities2.java:74)

Here you can find the XML file :

<?xml version="1.0" encoding="UTF-8" ?>

<repertoire>
<!-- John DOE -->
<personne sexe="masculin">
    <nom>DOE</nom>
    <prenom>John</prenom>
    <telephones>
        <telephone type="fixe">01 02 03 04 05</telephone>
        <telephone type="portable">06 07 08 09 10</telephone>
    </telephones>
</personne>

And then the code block :

import java.io.File;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class cities2 {
public static void main(final String[] args) {
    /*
     * Etape 1 : récupération d'une instance de la classe "DocumentBuilderFactory"
     */
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {
        /*
         * Etape 2 : création d'un parseur
         */
        final DocumentBuilder builder = factory.newDocumentBuilder();

    /*
     * Etape 3 : création d'un Document
     */
    final Document document= builder.parse(new File("cities2.xml"));



    /*
     * Etape 4 : récupération de l'Element racine
     */
    final Element racine = document.getDocumentElement();

    //Affichage de l'élément racine
    System.out.println("\n*************RACINE************");
    System.out.println(racine.getNodeName());

    /*
     * Etape 5 : récupération des personnes
     */
    final NodeList racineNoeuds = racine.getChildNodes();
    final int nbRacineNoeuds = racineNoeuds.getLength();

    for (int i = 0; i<nbRacineNoeuds; i++) {
        if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE) {
            final Element personne = (Element) racineNoeuds.item(i);

        //Affichage d'une personne
        System.out.println("\n*************PERSONNE************");
        System.out.println("sexe : " + personne.getAttribute("sexe"));

            /*
         * Etape 6 : récupération du nom et du prénom
         */
        final Element nom = (Element) personne.getElementsByTagName("nom").item(0);
        final Element prenom = (Element) personne.getElementsByTagName("prenom").item(0);






        //Affichage du nom et du prénom
        System.out.println("nom : " + nom.getTextContent());
       System.out.println("prénom : " + prenom.getTextContent());

        /*
         * Etape 7 : récupération des numéros de téléphone
         */
        final NodeList telephones = personne.getElementsByTagName("telephone");
        final int nbTelephonesElements = telephones.getLength();

        for(int j = 0; j<nbTelephonesElements; j++) {
            final Element telephone = (Element) telephones.item(j);

                    //Affichage du téléphone
                    System.out.println(telephone.getAttribute("type") + " : " + telephone.getTextContent());
        }
        }               
    }           
    }
    catch (final ParserConfigurationException e) {
        e.printStackTrace();
    }
    catch (final SAXException e) {
        e.printStackTrace();
    }
    catch (final IOException e) {
        e.printStackTrace();
    }       
}

}

In fact, the problem concern both lines :

 //Affichage du nom et du prénom
    System.out.println("nom : " + nom.getTextContent());
   System.out.println("prénom : " + prenom.getTextContent());

The XML file is read and parsed, because we can get all outputs before this last one ( I mean System.out.println("nom : " + nom.getTextContent());..). I use JDK 8u121, So it's updated, If you have any ideas it would be helpful. Thank you

5
  • Where is the XML file Commented Jan 19, 2017 at 0:55
  • I am sorry. Please find the XML file and also the code block above. Thank you Commented Jan 19, 2017 at 1:14
  • Possible duplicate of stackoverflow.com/questions/14014989/… Commented Jan 19, 2017 at 3:15
  • Thank you a lot. my problem was resolved through this solution Commented Jan 19, 2017 at 9:40
  • Please how can I add the tag as resolved problem Commented Jan 19, 2017 at 9:42

1 Answer 1

0

The presence of org.apache.xerces.dom in the stacktrace indicates that you are not using the DOM implementation in the JRE, but Apache Xerces. You get this kind of exception if you are using a very old version of Xerces that doesn't implement DOM Level 3. Either remove Xerces (so that the DOM implementation from the JRE is used) or upgrade it to a newer version.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.