如何基于库存文件名创建xml文件

时间:2012-09-07 17:38:16

标签: java

我有一个包含一些股票价格文件的文件夹,格式为stockA.txt 我正在尝试创建一个java程序,它将没有扩展名的文件名存储在xml文件中作为节点。 例如,file.xml应采用以下形式:

<stockA></stockA>
<stockB></stockB>

问题是我收到以下错误代码: 线程“main”中的异常org.w3c.dom.DOMException:HIERARCHY_REQUEST_ERR:尝试插入不允许的节点。

这是代码

    /*
 * 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<String> results = new ArrayList<String>();
        for (int i = 0; i < 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();
        }
    }
}

如何修复该错误? 谢谢。

1 个答案:

答案 0 :(得分:2)

您要添加两次元素,一次在根级别,另一次在Stocks标记中。你只需要做一次。

for (String item: results) {
    Element myElement = doc.createElement(item);
    // doc.appendChild(myElement); -> You don't need this one
    rootElement.appendChild(myElement);
    counter++;
}

附加到根元素就足够了。