使用Apache xercesc验证XML文件的Schema

本文介绍了一个使用Xerces C++库进行XML文件及其对应XSD模式验证的示例程序。通过命令行参数传递XML文件及XSD文件路径,程序能够检查XML文件是否符合指定的XSD模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

-------------------------booklist.xsd----------------------------------------

<?xml version="1.0" encoding="GB2312"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xs:element name="booklist">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="book" type="bookType" maxOccurs="unbounded"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:complexType name="bookType">
  <xs:sequence>
   <xs:element name="ISBN" type="xs:string"/>
   <xs:element name="title" type="xs:string"/>
   <xs:element name="authorlist" type="authorlistType"/>
   <xs:element name="price" type="xs:decimal"/>
  </xs:sequence>
  <xs:attribute name="classify" use="required">
   <xs:simpleType>
    <xs:restriction base="xs:string">
     <xs:enumeration value="社会科学"/>
     <xs:enumeration value="自然科学"/>
    </xs:restriction>
   </xs:simpleType>
  </xs:attribute>
 </xs:complexType>
 <xs:complexType name="authorlistType">
  <xs:sequence maxOccurs="4">
   <xs:element name="author" type="xs:string"/>
  </xs:sequence>
 </xs:complexType>
</xs:schema>

-------------------------------booklist.xml--------------------------------

<?xml version="1.0" encoding="GB2312"?>
<booklist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="booklist.xsd">
 <book classify="自然科学">
  <ISBN>7-302-12066-8</ISBN>
  <title>JAVA使用教程</title>
  <authorlist>
   <author>Herbert Scihldt</author>
   <author>马海军</author>
  </authorlist>
  <price>64.00</price>
 </book>
 <book classify="社会科学">
  <ISBN>7-5037-1978</ISBN>
  <title>投资学</title>
  <authorlist>
   <author>张中华</author>
   <author>谢老三</author>
  </authorlist>
  <price>19.00</price>
 </book>
</booklist>
使用如下命令行参数验证booklist.xml是否符合booklist.xsd

booklist.xml booklist.xsd

如果验证成功提示验证成功,否则出现异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值