[jdom-interest] Nesting of complex type

Grzegorz Kaczor grzegorz.kaczor at gmail.com
Sun Mar 16 13:41:05 PDT 2008


Hi,

everything works as expected - you add to the same root first folder
element, then file, then length - what is wrong here?

I would suggest writing variable names starting with lower case to
distinguish from class names. And I would to the directory listing
using the following pattern:

you have a method

Element dumpDirectory(File file) {

   here you create the XML element this way

   Element el = new Element("Folder");
   el.setAttribute("name", file.getName());

   then list children elements, and for each

   check if it is a directory...
   if (child.isDirectory()) {

      el.addContent(dumpDirectory(child));

   }
   else {
      el.addContent(child.getName());
   }


   ....
   return el;
}

Regards,
Grzegorz

2008/3/16, Sunderam P <sunderam08 at yahoo.com>:
> Hi,
> I am trying to create an XML with following structure.
>
> <DocRoot>
>     <Folder name="xyz">
>         <Folder name="abc">
>             <File>File2</File>
>              <File>File3</File>
>         </Folder>
>         <File>File1</File>
>     </Folder>
> </DocRoot>
>
> Following is the code snippet of what I have right now.
>
>     //Loop through the list of files & directory to generate XML file
>     private void GenerateXML(String []DirectoryListing) throws IOException{
>         int i=0;
>         File CheckContent;
>         XMLGenerator.setRootElement(XMLRoot);
>          File CurrentDirectory;
>         for(i=0;i< DirectoryListing.length; i++)
>         {
>               //file/directory
>             String FolderContent = DirectoryListing[i];
>             CheckContent = new File(FolderContent);
>              if (CheckContent.isFile() == true)
>              {
>                   Element Folder = new Element("Folder");
>                                 CurrentDirectory = new File (FolderContent);
>
> Folder.addContent(CurrentDirectory.getParent());
>                                 Element FileName = new Element("FileName");
>                                 FileName.addContent(FolderContent);
>                                 Element FileLength = new Element("Length");
>
> FileLength.addContent(String.valueOf(CheckContent.length()));
>                                 XMLRoot.addContent(Folder);
>                                 XMLRoot.addContent(FileName);
>                 XMLRoot.addContent(FileLength);
>              }
>
>             if (CheckContent.isDirectory()==true)
>             {
>                 String [] FileList = ReadDirectory(FolderContent);
>                 //Element Directory = new Element("SubDirectory");
>                // Directory.addContent(FolderContent);
>               //  XMLRoot.addContent(Directory);
>                 GenerateXML(FileList);
>             }
>         }
>     }
>
> I get the following XML from the above snippet
> <SharedFolder>
>     <Folder>Directory</Folder>
>     <FileName>text file.txt</FileName>
>      <Length>9</Length>
>      <Folder>Folder</Folder>
>      <FileName>text file2.txt</FileName>
>      <Length>9</Length>
> </SharedFolder>
>
> My directory is as follows
> C:\Directory\ text file.txt , Folder\, Folder\text file2.txt.
>
> Any suggestions?
>
> thanks
>
>
>
> ________________________________
> Never miss a thing. Make Yahoo your homepage.
>
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>


-- 
io non ho parole
ma ti cercherò



More information about the jdom-interest mailing list