Node type & XQuery result parsing

Hello,

I Have 8 children nodes of a element but when I use getLength() I get 16 nodes. There’s always a type 3 node between two nodes type 3, which are mine… What is my mistake and what do these nodes types correspond to?

Thanks a lot
lsioon

Hello Isioon,

I’m having a hard time picturing your scenario - could you please post an example of your XML documents and the code you are having a problem with?

Thanks,
Trevor.

Hello,

Sorry was not easy to understand :slight_smile:

So, I have an element which has subelements when I want to get how many subelements it has I get two times more than I should… the code is here…

NodeList listeNoeud7 = donneesUser.getChildNodes();
NodeList listeSousNoeud;
System.out.println(listeNoeud7.getLength());

In my case it should return 8 and in fact it returns 16. I have strange additional element that are of type 3.

So i have two questions:
- what are these additional elements
- what do these types correspond to

Thx a lot

Lsimon

If you examined the “value” of the unexpected nodes it would make clearer what they are. The nodes of type 3 are probably TEXT nodes that correspond to whitespace. Typically this is the whitespace between each element. I assume that your input is not 1 line of text without spaces?

hello ! Thax for your answer but I do still have troubles :frowning:

in fact you re right these strange nodes have spaces as value but the number of whitespaces is always different and I don’t know how to avoid these nodes from being created.

I looked in the XML file of data and there were no whitespace in the value fields. I m not sure to understand what do in XML “spaces between elements” means?

If I can’t avoid their creatin how could I distinguish them from the others.


thx a lot

Lionel

here are the data i used

<?xml version="1.0" encoding="windows-1252" ?>
- <ino:response xmlns:ino=“http://namespaces.softwareag.com/tamino/response2” xmlns:xql=“http://metalab.unc.edu/xql/”>
- <xq:query xmlns:xq=“http://namespaces.softwareag.com/tamino/XQuery/result”>
-
</xq:query>
- <ino:message ino:returnvalue=“0”>
ino:messagelinefetching cursor</ino:messageline>
</ino:message>
- <xq:result xmlns:xq=“http://namespaces.softwareag.com/tamino/XQuery/result”>
- xq:object
-
-
-
Monsieur
Jeker
Emmanuel
Jeremie
1977-08-12
-
Rue Fin
11
1895
Vionnaz
Valais

ejeker@eivd.ch
024 481 15 26

-
Monsieur
Simon
Lionel
Claude
1975-09-17
-
Rue des Alpes
1
1200
Rennens
Vaud

lsimon@eivd.ch
021 634 81 42

-
Monsieur
Etter
Frederic

1977-12-30
-


3966
Rechy
Valais

fetter@eivd.ch
027 458 24 29

-
Madame
Mayer
Sophie
Rose
1981-02-20
-
Chemin des Fleurettes
31
1007
Lausanne
Vaud

smayer@eivd.ch
021 616 06 22


</xq:object>
</xq:result>
- <ino:cursor ino:handle=“1”>
<ino:current ino:position=“1” ino:quantity=“1” />
</ino:cursor>
- <ino:message ino:returnvalue=“0”>
ino:messagelinecursor fetched</ino:messageline>
</ino:message>
</ino:response>

Hello Lionel,

if I may ask, what is the reason behind using DOM methods to get a count…?

I suspect that using Tamino queries to perform counts will give the results that you are looking for. (This approach is also likely to be faster and more scalable.)

Perhaps the following query would meet your requirements:

   for $pers in input()/Personne
   where $pers/@idLogin = "lsimon"
   return count($pers/*)</pre><BR>(Or the more compact version:<BR><pre class="ip-ubbcode-code-pre">   count(input()/Personne[@idLogin="lsimon"]/*)

)

I hope that helps,
Trevor.

hello Trevor,

Thank you for your answer. I will use XQuery for that but my porblem is that I have to parse all the nodes and when I do that I have too many nodes. it seems that the structure returned by my tamino request contain these nodes :

I have to get the data from an XQuery structure and then put it into an arraylist.

My Tamino version is 4.1.1.1.

I convert the data returned by XQuery into a dom:
domElement=(org.w3c.dom.Element) (iterateur.next()).getElement();

then I parse the data and in the arraylist returned I find these empty nodes… :

public ProfilBean recupererProfilR (String loginUti){

ConnexionSource connexionSrcDonnees=new ConnexionSource();
org.w3c.dom.Element donneesUser = connexionSrcDonnees.requeteDonneesUtilisateur(loginUti);

// get node list
NodeList listeNoeud = donneesUser.getChildNodes();

// to fill the array list recursively reading
// Xquery returned data
remplirListe(listeNoeud);

return profil;
}

private void remplirListe (NodeList listeATraiter){
int i=0;
NodeList sousListe=null;

// to parse every element and put tag name followed by value
// into array list
for (i=0;i<listeATraiter.getLength();i++){
if (listeATraiter.item(i).getNodeType()==1){
sousListe=listeATraiter.item(i).getChildNodes();
profil.setDonneesUti(listeATraiter.item(i).getNodeName());
remplirListe(sousListe);
}
else {
profil.setDonneesUti(listeATraiter.item(i).getNodeValue());
}
}
}

Do you have any idea, perhaps i’m doing something wrong but that seems really strange

thank you very much for any idea

Lionel

Hi Lionel,

I think that the simplest approach (and the one requiring the least changes!) is to follow Mark’s advice and just check the current value before you process it further.

For example, you could change the “remplirListe” method to something like this:

   private void remplirListe(NodeList listeATraiter) {
      int i=0;
      NodeList sousListe=null;

      // to parse every element and put tag name followed by value 
      // into array list
      for (i=0;i<listeATraiter.getLength();i++){
         if (listeATraiter.item(i).getNodeType()==1){ 
            sousListe=listeATraiter.item(i).getChildNodes();
            profil.setDonneesUti(listeATraiter.item(i).getNodeName());
            remplirListe(sousListe); 
         }
         else {
            String value = listeATraiter.item(i).getNodeValue().trim();  // <-- Changed
            if ( !value.equals("") ) {                                   // <-- Changed
               profil.setDonneesUti(value);                              // <-- Changed
            }
         }
      }
   }</pre>This will filter out all the nodes with empty values (empty after calling the trim() method).  I'm not sure that this is the most efficient approach though!<BR><BR>Perhaps it is also interesting for you to have a look at the XMLSerializer class provided with Xerces.  This can be used to convert a DOM object to a String representation.<BR>Something like this could be done:<BR><pre class="ip-ubbcode-code-pre">   import org.apache.xml.serialize.XMLSerializer;
   import java.io.StringWriter;
   ...
   Element domElement = (Element)(iter.next()).getElement();
// remplirListe(domElement.getChildNodes());

   StringWriter sw = new StringWriter();
   XMLSerializer serxml = new XMLSerializer();
   serxml.setOutputCharStream(sw);
   serxml.serialize(domElement);
   System.out.println(sw.toString());

I’m not sure what should happen to the values inside the “profil” object, so maybe this is useless to you!

What is happening to the values in the “profil.setDonneesUti” method?

Thanks and regards,
Trevor.

Hi Trevor,

I use it to fill a html Form. I do a
every modulo 2 (after the tag name and its value)

Thank you for all your suggestions.

In fact, I did use the same method as you describe in your post and it works.

Once more thank you for all

best regards
Lionel