[jdom-interest] creating and parsing files

Dave Bartmess dingodave at edingo.net
Sat Jul 12 13:13:29 PDT 2003


First, the Iterator will give you a list of all the content children,
therefore in sequence, it will give you "qqlil", "welili", "iotin", then
"cvbtin".. The setup of your XML doesn't really lend well to getting the
Username and Passwords together. Try putting them in a parent element
like:

<DataSet>
  <Logon>
    <Username>
    </Username>
    <Password>
    </Password>
  </Logon>
</DataSet>

That will give you the ability to iterate through the set, and get a
matched pair of Username and Password.

Also, as someone mentioned, in your loop, you don't do an itr.next()
call, which is what would put the (using the above XML) Logon element in
the loop.

Try this:

      Iterator itr = dataset.iterator();      
      StringBuffer full = new StringBuffer();
      while(itr.hasNext()) {
          StringBuffer full = new StringBuffer();
	  Element tmpelem = (Element)itr.next();
          full.append(tmpelem.getChildText("Username") + " ");
          full.append(tmpelem.getChildText("Password") + " ");
          System.out.println(full.toString());
      }

Note that the "full" StringBuffer is initialized inside the loop,
because in your code, the Username and Password would have been added to
the single instance of the StringBuffer, and you would have ended up
with:

"qqlil welil1980 iotin cvbtin1975 "


Hope this helps! Good luck!

On Sat, 2003-07-12 at 07:35, salil khanwalkar wrote:
> Hi,
> My xml file  looks like this --
> <?xml version="1.0" encoding="UTF-8"?>
> <DataSet>
>  <Username>qqlil</Username>
>  <Password>welil1980</Password>
>  <Username>iotin</Username>
>  <Password>cvbtin1975</Password>
> </DataSet >
> i copy the contents in a list and then iterate through it using a
> iterator. The while(),
>  loops infinitely and prints only the first element qqlil welil1980. I
> am not able to get the 
> second username and password.  Is my xml file structure wrong or the
> code for reading
> it is wrong?
> // part  of the code
>       SAXBuilder builder = new SAXBuilder();
>       Document doc = builder.build(input);
>       Element root = doc.getRootElement();
>       List dataset = doc.getContent();
>      // System.out.println(dataset);
>       
>       Iterator itr = dataset.iterator();      
>       StringBuffer full = new StringBuffer();
>       while(itr.hasNext()) {
>           full.append(root.getChildText("Username") + " ");
>           full.append(root.getChildText("Password") + " ");
>           System.out.println(full.toString());          
>       }
> Thank You,
> Salil.
> 
> 
> 
> ______________________________________________________________________
> Do you Yahoo!?
> SBC Yahoo! DSL - Now only $29.95 per month!
-- 
David A. Bartmess
Software Configuration Manager / Sr. Software Developer
eDingo Enterprises
http://edingo.net




More information about the jdom-interest mailing list