0

我正在为我自己的娱乐做一个项目,其中涉及将几个变量的内容输出到 XML 文件。但是,当我输入程序时,转换器只输出第一行(XML 标头),没有其他内容。saveData() 方法在 writeFile() 之前调用,并且我在调用 writeFile() 之前将变量的值输出到控制台,所以我知道它们有一个值。

下面的代码:

public class Output {   
    private static double citySizeMiles;
    private static double citySizeAcres;
    private CityType type;
    private static int gpLimit;
    private static long totalWealth;
    private static long cityPopulation;

    public static void saveData() {
        cityPopulation = CityGenerator.cityPop;
        citySizeMiles = City.getCitySizeMiles(cityPopulation);
        citySizeAcres = City.getCitySizeAcres(cityPopulation);
        gpLimit = City.getGoldLimit();
        totalWealth = CityGenerator.cityWealth;
    }

    public static void writefile() {
        try {           
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = docFactory.newDocumentBuilder();

            // Root Elements
            Document doc = builder.newDocument();
            Element root = doc.createElement("data");

            // Data Element
            Element data = doc.createElement("City");
            root.appendChild(data);

            Attr attr = doc.createAttribute("name");
            attr.setValue("Test");
            data.setAttributeNode(attr);

            // City size (sq miles)
            Element sizeMi = doc.createElement("sizeMiles");
            sizeMi.appendChild(doc.createTextNode(String.valueOf(citySizeMiles)));
            data.appendChild(sizeMi);

            // City size (acres)
            Element sizeAc = doc.createElement("sizeAcres");
            sizeAc.appendChild(doc.createTextNode(String.valueOf(citySizeAcres)));
            data.appendChild(sizeAc);

            // Population
            Element pop = doc.createElement("population");
            pop.appendChild(doc.createTextNode(String.valueOf(cityPopulation)));
            data.appendChild(pop);

            // GP limit
            Element gpLim = doc.createElement("gpLimit");
            gpLim.appendChild(doc.createTextNode(String.valueOf(gpLimit)));
            data.appendChild(gpLim);

            // Total fluid wealth
            Element wealth = doc.createElement("totalWealth");
            wealth.appendChild(doc.createTextNode(String.valueOf(totalWealth)));
            data.appendChild(wealth);

            // Write to XML file
            TransformerFactory tf = TransformerFactory.newInstance();
            tf.setAttribute("indent-number", 4);

            Transformer trans = tf.newTransformer();
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            trans.setOutputProperty(OutputKeys.METHOD, "xml");

            DOMSource source = new DOMSource(doc);
            //StreamResult result = new StreamResult(new File("D:\\test.xml"));

            StreamResult result = new StreamResult(System.out);

            trans.transform(source, result);

        } catch(ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch(TransformerException tfe) {
            tfe.printStackTrace();
        }
    }
}
4

1 回答 1

1

我不是 DOM 专家(我像避免瘟疫一样避免它,并鼓励其他人也这样做)但我认为您未能将根元素连接到文档节点。

于 2013-09-13T07:55:27.000 回答