以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  [求助]用dom4j通过schema验证xml文件时出问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=46251)


--  作者:rayearth
--  发布时间:4/29/2007 6:04:00 PM

--  [求助]用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();
 }
}


--  作者:rayearth
--  发布时间:4/29/2007 6:10:00 PM

--  
多说一句,在XMLSpy上验证通过了的,所以很郁闷啊
--  作者:flyfoxs
--  发布时间:4/29/2007 7:35:00 PM

--  
文件有点不好看,给你个建议,你把文件尽量的删,直到最后删除一些之后代码就正常了,那么就是这些代码出问题了。


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

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


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
281.250ms