1

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

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

问题是我收到以下错误代码:线程“主”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();
        }
    }
}

我该如何解决这个错误?谢谢你。

4

1 回答 1

2

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

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

附加到根元素就足够了。

于 2012-09-07T17:42:50.537 回答