[jdom-interest] creating and parsing files

Kenneth Ellefsen kellefsen at bergen.oilfield.slb.com
Mon Jul 14 00:04:16 PDT 2003


ops, reverse those trailing slashes :)

On Mon, 2003-07-14 at 08:57, Kenneth Ellefsen wrote:
> Or even:
> <DataSet>
>    <user username="user1" password="password1" \>
>    <user username="user2" password="password2" \>
> </DataSet>
> 
> Then use:
> String username = element.getAttributeValue("username");
> String password = element.getAttributeValue("password");
> 
> element representing a <user> element
> 
> Cheers
> 
> On Sat, 2003-07-12 at 22:13, Dave Bartmess wrote:
> > 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!
-- 
Mvh,

Kenneth Ellefsen                   | Tlf: (+47) 55985829
Schlumberger Information Solutions | www.sis.slb.com





More information about the jdom-interest mailing list