文件输出后的xml-schema失效
<?xml version="1.0" encoding="UTF-8"?> <teaching-plan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="plan.xsd"> <course id="a1"> <course-name>大学英语</course-name> <course-hour>20</course-hour> <exam-form>考试</exam-form> </course> <course id="a2"> <course-name>高等数学</course-name> <course-hour>70</course-hour> <exam-form>考试</exam-form> </course> <course id="a3"> <course-name>计算机应用基础</course-name> <course-hour>108</course-hour> <exam-form>上机考试</exam-form> </course> </teaching-plan>
<?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="course-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>
package com.imooc.dom4j; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; 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 planWriter { public void writerXml(){ String flie="E:\\xml\\lXml\\bin\\plan-schema.xml"; // String flie1="E:\\xml\\lXml\\bin\\plan-schema1.xml"; SAXReader reader=new SAXReader(); try { Document document=reader.read(flie); Element root=document.getRootElement(); Element course=root.addElement("course"); course.addAttribute("id", "a5"); course.addElement("course-name").setText("我不喜欢的语文"); course.addElement("course-hour").setText("24"); course.addElement("exam-form").setText("考试"); Writer writer=new OutputStreamWriter(new FileOutputStream(flie),"UTF-8"); document.write(writer); writer.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void readerXml() { String flie="E:\\xml\\lXml\\bin\\plan-schema.xml"; // String flie1="E:\\xml\\lXml\\bin\\plan-schema1.xml"; SAXReader reader=new SAXReader(); try { Document document=reader.read(flie); Element root=document.getRootElement(); List<Element> Course=root.elements("course"); for(Element course:Course) { Attribute att=course.attribute("id"); System.out.println(att.getText()); System.out.println(course.elementText("course-name")); System.out.println(course.elementText("course-hour")); System.out.println(course.elementText("exam-form")); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub planWriter pw=new planWriter(); pw.writerXml(); pw.readerXml(); } }
图一为没输出前的状态
图二为输出后的状态.为何输出后的xml-shema失效了
1
收起
正在回答 回答被采纳积分+1
2回答
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星