关于XML文件的写入数据的疑问
老师,我在进行读写xml文件的代码练习时,发现一个难以发现的很奇怪的错误。XML文件数据无误,读写代码也没报错,里面的文件路径也是一样可用的。我在进行读操作的时候很顺利读出了里面所有数据,可是写操作代码不知道是什么原因,第一次运行写入的java文件的时候是可以顺利执行程序的。可是写完之后,我发现XML文件居然变空了,这个时候再次运行读写的java程序会报错,说“文件提前结束”,搞不懂这是啥意思啊?既然读的代码可以顺利把里面的数据读出,说明代码应该没问题啊。
下面是我的代码和文件数据,以及第一次和第二次运行两个java程序的结果
2
收起
正在回答
3回答
同学的代码中,读取xml的代码没有问题,但是在写入xml文件时,应该加上writer.close(),如:
将流关闭的同时,刷新将内容写入xml文件。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
哆丶哆
2019-07-10 16:31:05
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | package 从网页搭建入门JavaWeb.步骤三JavaWeb入门.XML入门; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class XMLWriter { public static void main(String[] args) { // String file = // "D:/compiling_software/eclipse/eclipse/workspace/慕课网/src/从网页搭建入门JavaWeb/步骤三JavaWeb入门/XML入门/plan.xml"; String file = "src/从网页搭建入门JavaWeb/步骤三JavaWeb入门/XML入门/plan.xml" ; SAXReader reader = new SAXReader(); try { Document document = reader.read(file); Element root = document.getRootElement(); Element newElement = root.addElement( "course" ); Element newElementName = newElement.addElement( "course-name" ); newElementName.setText( "计算机网络" ); Element newElementHour = newElement.addElement( "calss-hour" ); newElementHour.setText( "80" ); Element newElementForm = newElement.addElement( "exam-form" ); newElementForm.setText( "考试" ); newElement.addAttribute( "id" , "TC004" ); Writer writer = new OutputStreamWriter( new FileOutputStream(file), "GBK" ); document.write(writer); System.out.println( "写入完毕" ); } catch (DocumentException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package 从网页搭建入门JavaWeb.步骤三JavaWeb入门.XML入门; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class XMLreader { public static void main(String[] args) { // String file = "d:/compiling_software/eclipse/eclipse/workspace/慕课网/src/从网页搭建入门JavaWeb/步骤三JavaWeb入门/XML入门/plan.xml"; String file = "src/从网页搭建入门JavaWeb/步骤三JavaWeb入门/XML入门/plan.xml" ; SAXReader reader = new SAXReader(); try { Document document = reader.read(file); Element root = document.getRootElement(); List<Element> elements = root.elements( "course" ); for (Element ele : elements) { Attribute att = ele.attribute( "id" ); String id = att.getText(); Element name = ele.element( "course-name" ); String course_name = name.getText(); Element hour = ele.element( "class-hour" ); String class_hour = hour.getText(); Element form = ele.element( "exam-form" ); String exam_form = form.getText(); System.out.println( "课程ID:" + id); System.out.println( "课程名称:" + course_name); System.out.println( "课程课时:" + class_hour); System.out.println( "考核方式:" + exam_form); } } catch (DocumentException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?xml version= "1.0" encoding= "UTF-8" ?> <schema xmlns= "http://www.w3.org/2001/XMLSchema" > <element name= "teaching-plan" > <complexType> <sequence> <element name= "course" maxOccurs= "100" > <complexType> <sequence> <element name= "course-name" type= "string" ></element> <element name= "class-hour" > <simpleType> <restriction base= "integer" > <minInclusive value= "20" ></minInclusive> <maxInclusive value= "110" ></maxInclusive> </restriction> </simpleType> </element> <element name= "exam-form" type= "string" ></element> </sequence> <attribute name= "id" type= "string" use= "required" ></attribute> </complexType> </element> </sequence> </complexType> </element> </schema> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version= "1.0" encoding= "UTF-8" ?> <teaching-plan xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "plan.xsd" > <course id= "TC001" > <course-name>高等数学</course-name> < class -hour> 36 </ class -hour> <exam-form>考试</exam-form> </course> <course id= "TC002" > <course-name>计算机二级</course-name> < class -hour> 48 </ class -hour> <exam-form>上机考试</exam-form> </course> <course id= "TC003" > <course-name>汇编语言</course-name> < class -hour> 106 </ class -hour> <exam-form>考试</exam-form> </course> </teaching-plan> |
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧