以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  利用schema验证xml问题,错误消息不能显示?  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=51576)


--  作者:chenxm21
--  发布时间:8/20/2007 6:02:00 PM

--  利用schema验证xml问题,错误消息不能显示?
我写了一个简单的用xsd验证xml的小例子,但是最后出来的错误消息,不是我需要的消息,老得到这样的错误消息:
http://www.w3.org/TR/xml-schema-1#cvc-complex-type.2.4.a?onth&{"":month}
希望能得到类似这样的消息:
The element type "month" must be terminated by the matching end-tag "</month>".
我觉得可能是由于读取消息resource文件的时候出现问题,我需要怎么做才能得到正确的消息?谢谢。

程序代码:
public static void main(String args[])
    {
        try {

            // Parse an XML document into a DOM tree.
            DocumentBuilder parser =
                DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = parser.parse(new File("D:\\temp\\test.xml"));

            // Create a SchemaFactory capable of understanding WXS schemas.
            SchemaFactory factory =
                SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            // Load a WXS schema, represented by a Schema instance.
//            Source schemaFile = new StreamSource(new File("D:\\temp\\test.xsd"));
//            Schema schema = factory.newSchema(schemaFile);
            
            File schemaLocation = new File("D:\\temp\\test.xsd");
            Schema schema = factory.newSchema(schemaLocation);
  
            // Create a Validator object, which can be used to validate
            // an instance document.
            Validator validator = schema.newValidator();

            // Validate the DOM tree.
            validator.validate(new DOMSource(document));
            
            

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            System.out.print(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }


XML文件内容:
<?xml version="1.0"?>
   
<birthdate>
    <onth>January</onth>
    <day>21</day>
    <year>1983</year>
</birthdate>


XSD内容:
<?xml version="1.0" encoding="UTF-8"?>
   
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"
        schemaLocation="http://www.w3.org/2001/xml.xsd" />

  <xs:element name="birthdate">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="month" type="xs:string" />
        <xs:element name="day" type="xs:int" />
        <xs:element name="year" type="xs:int" />
      </xs:sequence>  
    </xs:complexType>
  </xs:element>
   
</xs:schema>



--  作者:gemingke
--  发布时间:8/21/2007 9:17:00 PM

--  
你自己的文档错误 。。。

<onth>January</onth>


--  作者:lish
--  发布时间:9/12/2007 4:10:00 PM

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