dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | flex | CRM | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>java>开源框架>文章内容
JDOM、Schema验证xml文件
来源: 作者:

一、xml文件 

<?xml version="1.0" encoding="GB2312"?>
<!-- edited with XML Spy v4.3 U (http://www.xmlspy.com) by Ke Deng (Sun) -->
<问题群 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <问题 ID="1" Date="2002-03-18" Section="java.awt package" Type="Choicer">
  <回答>
   <Item IsTrue="true">同志们</Item>
  </回答>
  <Content>sdfwer</Content>
  <Comment>rwrwe</Comment>
 </问题>
</问题群>

 
二、test-schema.xml

<?xml version="1.0" encoding="GB2312"?>
<!-- edited with XML Spy v4.3 U (http://www.xmlspy.com) by Ke Deng (Sun) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="问题群">
  <xs:annotation>
   <xs:documentation>Comment describing your root element</xs:documentation>
  </xs:annotation>
  <xs:complexType>
   <xs:all>
    <xs:element ref="问题"/>
   </xs:all>
  </xs:complexType>
 </xs:element>
 <xs:element name="问题">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="回答"/>
    <xs:element name="Content">
     <xs:simpleType>
      <xs:restriction base="xs:string">
       <xs:whiteSpace value="preserve"/>
      </xs:restriction>
     </xs:simpleType>
    </xs:element>
    <xs:element name="Comment" type="xs:string"/>
   </xs:sequence>
   <xs:attribute name="ID" type="xs:int" use="required"/>
   <xs:attribute name="Provider" type="xs:string" use="optional"/>
   <xs:attribute name="Date" type="xs:date" use="required"/>
   <xs:attribute name="Section" type="xs:string" use="required"/>
   <xs:attribute name="Type" use="required">
    <xs:simpleType>
     <xs:restriction base="xs:string">
      <xs:enumeration value="Choicer"/>
      <xs:enumeration value="Filler"/>
     </xs:restriction>
    </xs:simpleType>
   </xs:attribute>
  </xs:complexType>
 </xs:element>
 <xs:element name="回答">
  <xs:complexType>
   <xs:all>
    <xs:element name="Item">
     <xs:complexType>
      <xs:simpleContent>
       <xs:extension base="xs:string">
        <xs:attribute name="IsTrue" type="xs:boolean" use="optional" fixed="true"/>
       </xs:extension>
      </xs:simpleContent>
     </xs:complexType>
    </xs:element>
   </xs:all>
  </xs:complexType>
 </xs:element>
</xs:schema>

三、程序:

import java.io.*;
 import org.jdom.*;
 import org.jdom.input.*;
 import org.jdom.output.*;

 public class XMLWithSchema
 {
  String xml="test.xml";
  String schema="test-schema.xml";

   public static void main(String args[]){
      XMLWithSchema xws=new XMLWithSchema();
      xws.validate();
  }
  public void validate()
  {
   try
   {
    SAXBuilder builder = new SAXBuilder(true);
    //指定约束方式为XML schema
    builder.setFeature("http://apache.org/xml/features/validation/schema",true);
    //导入schema文件
    builder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",schema); 
    Document doc = builder.build(new FileReader(xml));
    
    System.out.println("OK");
     Format format = Format.getCompactFormat();

        format.setEncoding("gb2312");           //设置xml文件的字符为gb2312

        format.setIndent("    ");               //设置xml文件的缩进为4个空格


    XMLOutputter outputter = new XMLOutputter(format);
    outputter.output(doc, System.out);
    outputter.output(doc, new FileOutputStream("s.xml")); 

   }catch(Exception e)
   {
    System.out.println("验证失败:"+e);
   }
   
  }
  
 }

运行结果:


C:\Test>java XMLWithSchema
OK
<?xml version="1.0" encoding="gb2312"?>
<!-- edited with XML Spy v4.3 U (http://www.xmlspy.com) by Ke Deng (Sun) -->
<问题群 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <问题 ID="1" Date="2002-03-18" Section="java.awt package" Type="Choicer">
        <回答>
            <Item IsTrue="true">同志们</Item>
        </回答>
        <Content>sdfwer</Content>
        <Comment>rwrwe</Comment>
    </问题>
</问题群>


C:\Test>


 
 
上一篇:SAXBuilder,Document,Element等作用   下一篇: 如何通过java或jsp向数据库存取二进制图片
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·关于JSF和Struts的讨论
·Struts教程-Struts模块化编程教
·Struts入门经验
·用科学的思维方法指导软件的设计
·Hibernate配置文件中映射元素详
·Spring中事件处理的小技巧
·struts2.0pring2.0 hibernate3.2
·struts2.0 spring2.0 hibernate3
·浅谈hibernate lazy fetch
·Hibernate的Fetch
·优化hibernate性能的几点建议
·Hibernate中的取策略延迟加载
  相关文章
·SAXBuilder,Document,Element
·JDOM入门
·hibernate增删改查代码实例
·学习装饰者模式(Decorator)
·DAO设计模式
·jfreechart例子
· Tomcat下使用Jfreechart 简单例
·JFreeChart使用心得与例子
·使用JFreeChart生成热点图表
·JFreeChart在JSP中的应用实例
·JFreeChart初学者入门实例详细解
·JFreeChart教程 -- 入门
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763