【问题标题】:Why cant I see my xml file? When I run it, it executes with nothing there to show为什么我看不到我的 xml 文件?当我运行它时,它执行时没有任何显示
【发布时间】:2015-03-03 00:39:20
【问题描述】:
//The import files in use

import java.io.FileWriter;
import java.io.IOException;
import javax.xml.stream.*;

public class WriteXML {

    public static void main(String[] args) {
        WriteXML t = new WriteXML();
        t.m();
    }

    private void m() {

        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

        try {
        // Create the XMLStreamWriter object   
            // this is where it should create my file for xml

            FileWriter fileWriter = new FileWriter("products.xml");
            XMLStreamWriter writer = outputFactory.createXMLStreamWriter(fileWriter);

            //Here is the actual xml data        
            writer.writeStartDocument("1.0")   
            writer.writeComment("Product data");
            writer.writeStartElement("Products");

            writer.writeStartElement("Product");
            writer.writeAttribute("Code", "Java");

            writer.writeStartElement("Description");
            writer.writeCharacters("Java SE 6.0");
            writer.writeEndElement();
            // for the Product element

            writer.writeEndElement();
            writer.writeStartElement("Product");
            writer.writeAttribute("Code", "C++");

            writer.writeStartElement("Description");
            writer.writeCharacters("C++ Programming");
            writer.writeEndElement();
            // for the Product element

            writer.writeEndElement();
            writer.flush();
            writer.close();
        } //catch statements
        catch (IOException e) {
            e.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 代码转储,不是问题。 products.xml 将被写入当前工作目录。您可以使用new File(".").getAbsolutePath() 并将其打印到控制台以确定程序的执行位置...
  • 如何将其合并到当前代码中?

标签: java xml


【解决方案1】:

文件可能是在您不期望的目录中创建的,例如the current working directory。尝试指定绝对路径。

Windows 示例:

C:\Users\Walter\Desktop\products.xml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    相关资源
    最近更新 更多