Réf. : Re: GetChildNodes

"Pascal EMMA (laltruiste)" <[email protected]>
Newsgroups gmane.text.xml.french.tech
Message-ID <451A1084.000001.03880@ID-POSTE-1>
Bonjour,
Il faut toujours tester le type des noeuds avant de les "caster" avec ceci :

if(children.item(i).getNodeType() == Node.ELEMENT_NODE){
    Element elt = (Element)children.item(i); 
    //...
}

 Si ce test n'est pas effectué, alors vous aurez droit à des exceptions dans
le cas ou les noeuds seraient du type NODE_TEXT ou autres.

Cordialement,
Pascal EMMA

-------Message original-------
 
De : [email protected]
Date : 09/26/06 22:20:08
A : [email protected]
Sujet : [xml-tech] Re: GetChildNodes
 
Oui pardon, vous avez raison. Je ne sais pas si je peux tout expliquer comme
ca:
Dans le fichier XML dimRuleElt a pour valeur l'élément CHILD qui a pour id
"Human Resources". C'est bien pour cela que je ne comprends pas pourquoi cet
élément a 4fils. Il devrait en avoir que 2. Et apparemment j'ai une
exception Cast en faisant ca: Element elt = (Element)children.item(i); et je
ne vois pas pourquoi...
 
Merci pour votre reponse
Sophie
 
-----Message d'origine-----
De : [email protected] [mailto:[email protected]
>mailto:[email protected]">[email protected]
[mailto:[email protected]]
Envoyé : mardi 26 septembre 2006 17:07
À : '[email protected]'
Objet : [xml-tech] GetChildNodes
 
Hello,
 
I hope you will agree to help me. I get into troubles while getting the
children of a node.
 
Here is the part of my XML that I want to parse:
 
<child id="Human Resources" level="70" active="true">
 
<labels>
<label xml:lang="en">Human Resources</label>
<label xml:lang="fr">Ressources Humaines</label></labels>
 
<children>
 
    <child id="Human Resources Ost Schweiz" level="60" active="true">
 
    <labels>
    <label xml:lang="en">Human Resources Ost Schweiz</label>
     <label xml:lang="fr">Ressources Humaines Suisse
Est</label></labels></child>
 
     <child id="Human Resources West Schweiz" level="60" active="true">
 
      <labels>
      <label xml:lang="en">Human Resources West Schweiz</label>
       <label xml:lang="fr">Ressources Humaines Suisse
Ouest</label></labels></child></children></child></children></child></childr
en></child>
 
And here is the code:
 
   public List<String> getDimChildren(Element dimRuleElt, int nbChild)
  {
   List<String> listIds = new ArrayList<String>();
 
   try{
 
     int iter = 0;
     while(dimRuleElt.hasChildNodes() && iter<nbChild)
     {
     NodeList children =
dimRuleElt.getChildNodes();
     logger.info("children
size"+children.getLength());    // I AM GETTING 4 CHILDREN WHY?
 
     for(int i=0; i<children.getLength(); i++)
     {
     Element elt =
(Element)children.item(i);
// WHY IS IT WRONG HERE? I CAN DO THIS CAST
     String tagname = elt.getTagName();
     logger.info("tagname= "+tagname);
     if(tagname.equals("child"))
     {
     String name =
elt.getAttribute("id");
     listIds.add(name);
     }
 
     listIds.addAll(getDimChildren(elt,
nbChild-1));
     }
     }
 
   }catch(Exception e){
    System.err.println("Exception :");
    e.printStackTrace();
   }
   return listIds;
  }
 
And here is the error I am getting:
java.lang.ClassCastException: org.apache.xerces.dom.DeferredTextImpl
 
Do you see what I am doing wrong here?
Thanks
Sophie
 
 
--
Devenez redacteur <XML>fr et contribuez au developpement du
xml francophone (http://xmlfr.org/infos/redacteurs/) !
 
Liste de diffusion "[email protected]" (http://xmlfr.org).
 
Cette liste est a votre disposition pour discuter en francais de
tout sujet technique lie a XML.
 
Pour resilier votre abonnement, envoyez un message contenant
la commande "unsubscribe" a [email protected]
(mailto:[email protected]?Subject=unsubscribe)
 
 
 
--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.12.9/456 - Release Date: 25/09/2006
 
.

-- Binary/unsupported file stripped by Ecartis --
-- Err : No filename to use for decode, file stripped.
-- Type: image/gif
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.