新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> DTD, XML Schema(XMLS), RELAX NG
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 DTD/XML Schema 』 → [求助]用dom4j通过schema验证xml文件时出问题 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7833 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [求助]用dom4j通过schema验证xml文件时出问题 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     rayearth 美女呀,离线,快来找我吧!
      
      
      等级:大一新生
      文章:5
      积分:77
      门派:XML.ORG.CN
      注册:2007/1/23

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给rayearth发送一个短消息 把rayearth加入好友 查看rayearth的个人资料 搜索rayearth在『 DTD/XML Schema 』的所有贴子 引用回复这个贴子 回复这个贴子 查看rayearth的博客楼主
    发贴心情 [求助]用dom4j通过schema验证xml文件时出问题

    大家帮忙看一下吧,先谢了!
    比较急,大家多帮忙了!

    error:
    ****************
    <errors>
      <error column="63" line="2">cvc-elt.1: Cannot find the declaration of element 'resource-lists'.</error>
      <error column="224" line="2" systemID="file:///D:/xmltest/schema/rl.xsd">TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace of 'urn:ietf:params:xml:ns:resource-lists'.</error>
      <error column="224" line="2" systemID="file:///D:/xmltest/schema/rl.xsd">TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace of 'urn:ietf:params:xml:ns:resource-lists'.</error>
      <error column="224" line="2" systemID="file:///D:/xmltest/schema/rl.xsd">TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace of 'urn:ietf:params:xml:ns:resource-lists'.</error>
      <error column="224" line="2" systemID="file:///D:/xmltest/schema/rl.xsd">TargetNamespace.2: Expecting no namespace, but the schema document has a target namespace of 'urn:ietf:params:xml:ns:resource-lists'.</error>
    </errors>
    --------------------------------------------------------------------------------------------------------------------
    schema:
    ****************
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns="urn:ietf:params:xml:ns:resource-lists" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:ietf:params:xml:ns:resource-lists" elementFormDefault="qualified" attributeFormDefault="unqualified">
     <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/>
     <xs:complexType name="listType">
      <xs:sequence>
       <xs:element name="display-name" type="display-nameType" minOccurs="0"/>
       <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:choice>
         <xs:element name="list">
          <xs:complexType>
           <xs:complexContent>
            <xs:restriction base="listType"/>
           </xs:complexContent>
          </xs:complexType>
         </xs:element>
         <xs:element name="external" type="externalType"/>
         <xs:element name="entry" type="entryType"/>
         <xs:element name="entry-ref" type="entry-refType"/>
        </xs:choice>
       </xs:sequence>
       <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="name" type="xs:string" use="optional"/>
      <xs:anyAttribute namespace="##other"/>
     </xs:complexType>
     <xs:complexType name="entryType">
      <xs:sequence>
       <xs:element name="display-name" minOccurs="0">
        <xs:complexType>
         <xs:simpleContent>
          <xs:extension base="display-nameType"/>
         </xs:simpleContent>
        </xs:complexType>
       </xs:element>
       <xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="uri" type="xs:anyURI" use="required"/>
      <xs:anyAttribute namespace="##other"/>
     </xs:complexType>
     <xs:complexType name="entry-refType">
      <xs:sequence>
       <xs:element name="display-name" type="display-nameType" minOccurs="0"/>
       <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="ref" type="xs:anyURI" use="required"/>
      <xs:anyAttribute namespace="##other"/>
     </xs:complexType>
     <xs:complexType name="externalType">
      <xs:sequence>
       <xs:element name="display-name" type="display-nameType" minOccurs="0"/>
       <xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="anchor" type="xs:anyURI"/>
      <xs:anyAttribute namespace="##other"/>
     </xs:complexType>
     <xs:element name="resource-lists">
      <xs:complexType>
       <xs:sequence maxOccurs="unbounded">
        <xs:element name="list" type="listType"/>
       </xs:sequence>
      </xs:complexType>
     </xs:element>
     <xs:complexType name="display-nameType">
      <xs:simpleContent>
       <xs:extension base="xs:string">
        <xs:attribute ref="xml:lang"/>
       </xs:extension>
      </xs:simpleContent>
     </xs:complexType>
    </xs:schema>
    --------------------------------------------------------------------------------------------------------------------
    xml文件:
    ***************
    <?xml version="1.0" encoding="UTF-8"?>
    <resource-lists xmlns="urn:ietf:params:xml:ns:resource-lists">
     <list name="Address List">
      <entry uri="sip:gaopeng@example.com">
       <display-name>gaopeng</display-name>
      </entry>
      <entry uri="sip:fantao@example.com">
       <display-name>fantao</display-name>
      </entry>
      <entry uri="sip:tengshengbo3@example.com">
       <display-name>tengshengbo</display-name>
      </entry>
     </list>
    </resource-lists>
    --------------------------------------------------------------------------------------------------------------------
    java程序:
    ****************
    package com.test.dom4j;

    import java.io.ByteArrayInputStream;
    import java.io.FileReader;

    import org.dom4j.Document;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    import org.dom4j.util.XMLErrorHandler;

    public class ValidateSchema {

     public void useSchema(){
      String schemaUrl="D:/xmltest/schema/rl.xsd"; //该url得到一个schema的xsd文件。
      String outString = null;
      
      char []cbuf = new char[1024];
      try{
       FileReader fr = new FileReader("D:/xmltest/xml/addresslist.xml");       
       fr.read(cbuf);
      }
      catch(Exception e){e.printStackTrace();}
      
            String content = new String(cbuf).trim();
            
      try{  
       SAXReader reader = new SAXReader(true);
       reader.setFeature("http://xml.org/sax/features/validation", true);
       reader.setFeature(
         "http://apache.org/xml/features/validation/schema", true);

       reader .setProperty(
         "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
         schemaUrl );
                   
       XMLErrorHandler errorHandler = new XMLErrorHandler();
       reader.setErrorHandler(errorHandler);
      
       Document document = reader.read(new ByteArrayInputStream(content.getBytes("utf-8")));
       if (errorHandler.getErrors().hasContent()) {
        
        XMLWriter writer = new XMLWriter( OutputFormat.createPrettyPrint() );
        writer.write( errorHandler.getErrors());
       }
       else {
        outString = "validate success.";
       }
       
      } catch (Exception e) {
       
       outString = "validate XML Document Exception :\n" + e;
       
      } finally {
       
       System.out.println("\n" + outString);
       

      }
     }
     
     public static void main(String[] args){
      ValidateSchema vs = new ValidateSchema();
      
      vs.useSchema();
     }
    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/4/29 18:04:00
     
     rayearth 美女呀,离线,快来找我吧!
      
      
      等级:大一新生
      文章:5
      积分:77
      门派:XML.ORG.CN
      注册:2007/1/23

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给rayearth发送一个短消息 把rayearth加入好友 查看rayearth的个人资料 搜索rayearth在『 DTD/XML Schema 』的所有贴子 引用回复这个贴子 回复这个贴子 查看rayearth的博客2
    发贴心情 
    多说一句,在XMLSpy上验证通过了的,所以很郁闷啊
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/4/29 18:10:00
     
     flyfoxs 帅哥哟,离线,有人找我吗?
      
      
      威望:5
      等级:研一(Artificial Intelligence期期不放过)
      文章:550
      积分:3935
      门派:XML.ORG.CN
      注册:2005/1/8

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给flyfoxs发送一个短消息 把flyfoxs加入好友 查看flyfoxs的个人资料 搜索flyfoxs在『 DTD/XML Schema 』的所有贴子 引用回复这个贴子 回复这个贴子 查看flyfoxs的博客3
    发贴心情 
    文件有点不好看,给你个建议,你把文件尽量的删,直到最后删除一些之后代码就正常了,那么就是这些代码出问题了。


    虽然这样不一定能解决问题,但至少可以找到问题。

    想帮你找找疸,但没有环境,不太好帮你找问题。

    ----------------------------------------------
    存在即是被搜索!

    BLOG =>  http://www.OpenJ.cn

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/4/29 19:35:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DTD/XML Schema 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/13 17:02:26

    本主题贴数3,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    82.031ms