关于XML文件的写入数据的疑问

关于XML文件的写入数据的疑问

老师,我在进行读写xml文件的代码练习时,发现一个难以发现的很奇怪的错误。XML文件数据无误,读写代码也没报错,里面的文件路径也是一样可用的。我在进行读操作的时候很顺利读出了里面所有数据,可是写操作代码不知道是什么原因,第一次运行写入的java文件的时候是可以顺利执行程序的。可是写完之后,我发现XML文件居然变空了,这个时候再次运行读写的java程序会报错,说“文件提前结束”,搞不懂这是啥意思啊?既然读的代码可以顺利把里面的数据读出,说明代码应该没问题啊。

下面是我的代码和文件数据,以及第一次和第二次运行两个java程序的结果

http://img1.sycdn.imooc.com//climg/5d249481000160fb09690914.jpg

http://img1.sycdn.imooc.com//climg/5d2494820001c9cb09690914.jpg

http://img1.sycdn.imooc.com//climg/5d24948200019bb809690914.jpg

http://img1.sycdn.imooc.com//climg/5d249484000146cf05960396.jpg

http://img1.sycdn.imooc.com//climg/5d249484000157e109690914.jpg

http://img1.sycdn.imooc.com//climg/5d2494840001008605960396.jpg

http://img1.sycdn.imooc.com//climg/5d2494860001899909690914.jpg

http://img1.sycdn.imooc.com//climg/5d2494860001c12618520483.jpg

http://img1.sycdn.imooc.com//climg/5d2494860001bcf718520478.jpg


正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学的代码中,读取xml的代码没有问题,但是在写入xml文件时,应该加上writer.close(),如:

http://img1.sycdn.imooc.com//climg/5d25c0970001417706140498.jpg

将流关闭的同时,刷新将内容写入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>


好帮手慕阿满 2019-07-10 10:45:41

同学你好,建议同学将代码贴上来,方便我们具体测试,贴代码时记得贴在我要回答中。另外建议同学在命名时,不要使用中文。

祝:学习愉快~


  • 提问者 哆丶哆 #1
    好的,可是代码太长没法贴出来
    2019-07-10 13:43:10
  • 好帮手慕阿满 回复 提问者 哆丶哆 #2
    同学你好,这里只需要将同学的读和写xml的代码贴在回答中即可。在回答中,选择代码语言,将代码贴上来。(不是截图哦)。祝:学习愉快~
    2019-07-10 14:51:36
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码