Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何添加standalone=";否";使用JDOM创建一个文件?_Java_Xml_Jdom 2 - Fatal编程技术网

Java 如何添加standalone=";否";使用JDOM创建一个文件?

Java 如何添加standalone=";否";使用JDOM创建一个文件?,java,xml,jdom-2,Java,Xml,Jdom 2,我找到了如何将XML文档的打印输出覆盖到Eclipse控制台,使其包含standalone=“no”,但如何将standalone=“no”写入文件?我已尝试将同一文档写入文件,但它仍然无法单独打印=“否”。换句话说,当我尝试写入文件时,重写的方法不起作用 在写入文件时,是否有其他方法需要重写?这里的问题是什么 private static void writeXML() { try { Document doc = new Document(); Element theRoot = ne

我找到了如何将XML文档的打印输出覆盖到Eclipse控制台,使其包含standalone=“no”,但如何将standalone=“no”写入文件?我已尝试将同一文档写入文件,但它仍然无法单独打印=“否”。换句话说,当我尝试写入文件时,重写的方法不起作用

在写入文件时,是否有其他方法需要重写?这里的问题是什么

private static void writeXML() {

try {

Document doc = new Document();

Element theRoot = new Element("tvshows");
doc.setRootElement(theRoot);

Element show = new Element("show");
Element name = new Element("name");
name.setAttribute("show_id", "show_001");

name.addContent(new Text("Life on Mars"));

Element network = new Element("network");
network.setAttribute("country", "US");

network.addContent(new Text("ABC"));

show.addContent(name);
show.addContent(network);

theRoot.addContent(show);

//-----------------------------

Element show2 = new Element("show");
Element name2 = new Element("name");
name2.setAttribute("show_id", "show_002");

name2.addContent(new Text("Life on Mars"));

Element network2 = new Element("network");
network2.setAttribute("country", "UK");

network2.addContent(new Text("BBC"));

show2.addContent(name2);
show2.addContent(network2);

theRoot.addContent(show2);

XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat(), XMLOUTPUT);
//xmlOutput.output(doc, System.out);

xmlOutput.output(doc, new FileOutputStream(new File("./src/jdomMade.xml")));

System.out.println("The file has been written");

}
catch (Exception ex){
    ex.printStackTrace();
}


}

public static final XMLOutputProcessor XMLOUTPUT = new AbstractXMLOutputProcessor() {
@Override
protected void printDeclaration(final Writer out, final FormatStack fstack) throws IOException {
    write(out, "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?> ");
    write(out, fstack.getLineSeparator());
}

};
私有静态void writeXML(){
试一试{
单据单据=新单据();
元素theRoot=新元素(“tvshows”);
文档setRootElement(theRoot);
元素显示=新元素(“显示”);
元素名称=新元素(“名称”);
name.setAttribute(“show_id”、“show_001”);
name.addContent(新文本(“火星上的生命”);
元素网络=新元素(“网络”);
network.setAttribute(“国家”、“美国”);
添加内容(新文本(“ABC”));
show.addContent(名称);
show.addContent(网络);
theRoot.addContent(show);
//-----------------------------
元素show2=新元素(“show”);
元素名称2=新元素(“名称”);
name2.setAttribute(“show_id”、“show_002”);
名称2.添加内容(新文本(“火星上的生命”);
元素网络2=新元素(“网络”);
network2.setAttribute(“国家”、“英国”);
网络2.添加内容(新文本(“BBC”);
show2.addContent(name2);
show2.addContent(network2);
theRoot.addContent(如图2所示);
XMLOutputer xmlOutput=新的XMLOutputer(Format.getPrettyFormat(),xmlOutput);
//xmlOutput.output(doc,System.out);
输出(doc,新文件outputstream(新文件(“./src/jdomMade.xml”));
System.out.println(“文件已写入”);
}
捕获(例外情况除外){
例如printStackTrace();
}
}
公共静态最终XMLOutputProcessor XMLOUTPUT=新的AbstractXMLOutputProcessor(){
@凌驾
受保护的void printDeclaration(最终写入器输出、最终FormatStack fstack)引发IOException{
写上“”;
写入(out,fstack.getLineSeparator());
}
};
您的代码存在;-)

println说文件已经写了,但还没有

仅当文件被刷新并关闭时,才会写入该文件。你不能那样做

您应该在代码中添加资源试用:

try (FileOutputStream fos = new FileOutputStream(new File("./src/jdomMade.xml"))) {
    xmlOutput.output(doc, fos);
}

System.out.println("The file has been written");

可能是Hmmm.Apon的副本仔细阅读,您的问题不是单机版,而是将其写入文件。你试过什么?你能把你的代码复制到这里吗?我用一个Writer运行代码,OutputStream对我有用。请看这里的pastebin:好的,我在代码中添加了。当发送到System.out[xmlOutput.output(doc,System.out);]时,将覆盖printDeclaration()方法,但当流式传输到文件xmlOutput.output(doc,new FileOutputStream(new file(“./src/jdomMade.xml”);,它没有被覆盖。我查看文件,每当我写入它时,它都会更新。@raisinte-我已经复制了你的代码,运行了它,等等。所有这些都对我有用。我看到的唯一问题是没有冲水/关闭。你确定它正在更新吗?每次运行时添加新的更改。。。。添加时间戳或唯一编号。。。。确保它与你在println中输入的值匹配。哈哈,我发现了问题!我正在用Internet Explorer打开文件。它会自动删除[standalone=“no”]。我刚用记事本打开文件,它就在那里!哎呀。谢谢你的帮助。嗯,谢谢,我想。今天我学到了一些新的东西——Internet Explorer也在撒谎;-)
try (FileOutputStream fos = new FileOutputStream(new File("./src/jdomMade.xml"))) {
    xmlOutput.output(doc, fos);
}

System.out.println("The file has been written");