1 Reply Latest reply: Jun 4, 2009 11:11 PM by 794254 RSS

    Generate XML file Dynamically

    807588
      We want to generate an xml file for the given folder in file system. This XML should contain nested nodes for all subfolders and files. Please could any one help me with logic to generate xml?

      Sample of XML
      <Parent_Folder>
      <Sub_Folder>
      <Sub_Sub_Folder/>
      </Sub_Folder>
      <Sub_File/>
      </Parent_Folder>

      Thanks in advance.
        • 1. Re: Generate XML file Dynamically
          794254
          Welcome to the Sun forums.

          1. Please post your code using code tags, this formats it so that it is easier for people to read and understand it. Click on 'CODE' above the text area when posting.
          2. As far as possible, please post an SSCCE
          3. Please explain what you are trying, what you're expecting and what you're getting. Don't expect to be spoon-fed the answer without putting in any work.

          The very simplest way would be to simply write all the nodes yourself:
          FileWriter writer = new FileWriter(new File("myXML.xml"));
          
          writer.write("<Parent_Folder>");
          writer.write("<Sub_Folder>");
          writer.write(subFolderFileObject.getName());
          writer.write("</Sub_Folder>");
          
          //etc
          But this is pretty brittle and prone to errors and would only work for the most trivial files. If you want robust XML support then you should use some library like perhaps jdom