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 : sjarlier@stradefi.com [mailto:sjarlier@stradefi.com]
Envoyé : mardi 26 septembre 2006 17:07
À : 'xml-tech@xmlfr.org'
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 "xml-tech@xmlfr.org" (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 xml-tech-request@xmlfr.org
(mailto:xml-tech-request@xmlfr.org?Subject=unsubscribe)
Received on Tue Sep 26 22:19:43 2006