How to create an xml file based on stock filenames
<p>I have a folder with some stock price files in the form stockA.txt
I am trying to create a java program that stores the filename without the extension in an xml file as a node.
For example the file.xml should be in the form :</p>

<pre><code>&lt;stockA&gt;&lt;/stockA&gt;
&lt;stockB&gt;&lt;/stockB&gt;
</code></pre>

<p>The problem is that I am getting the following error code :
Exception in thread "main" org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. </p>

<p>Here is the code</p>

<pre><code>    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlfilecreation;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author Administrator
 */
public class XMLFileCreation {



    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        File folder = new File("/Users/Administrator/Documents/TM470/NYSE_EOD/NyseLast2Years/");
        File[] listOfFiles = folder.listFiles();

        List&lt;String&gt; results = new ArrayList&lt;String&gt;();
        for (int i = 0; i &lt; listOfFiles.length; i++)
        {
            results.add(listOfFiles[i].getName().replace(".txt", ""));
            //System.out.println(results.get(i));
        }

        try
        {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            //elements
            Document doc = docBuilder.newDocument();

            //root element
            Element rootElement = doc.createElement("Stocks");
            doc.appendChild(rootElement);

            int counter = 0;

            Element myElement;
            for (String item: results)
            {
                myElement = doc.createElement(item);
                doc.appendChild(myElement);
                rootElement.appendChild(myElement);
                counter++;
            }

            System.out.println(counter);

            //write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("file.xml"));

            transformer.transform(source, result);

            System.out.println("File saved!");
        }
        catch (ParserConfigurationException ex)
        {
            ex.printStackTrace();
        }
        catch(TransformerException ex)
        {
            ex.printStackTrace();
        }
    }
}
</code></pre>

<p>How can I fix that error?
Thank you.</p>
