Re: GetChildNodes

<[email protected]>
Newsgroups gmane.text.xml.french.tech
Message-ID <001601c6e1a9$0da6ddc0$0301a8c0@JARLIER>
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]] 
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
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.