为什么我看不到我的xml文件?当我运行它时,它执行时没有显示任何内容

时间:2015-03-03 00:39:20

标签: java xml

//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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

Windows示例:

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