使用XSD验证xml
来源:互联网 发布:软件培训去哪里 编辑:程序博客网 时间:2024/04/29 06:44
使用XSD验证XML
本文简单介绍如何使用XSD验证自己的xml。项目中我们经常使用xml作为配置或者数据交互的文件格式,在解析XML前最好验证一下或者在生成xml文件后验证一下生成的文件格式是正确,这样可以避免解析时出现一些低级的xml文件不符合预定义格式的错误。
CSDN无法正确显示这些xsd, xml以及java代码,如果需要请参考http://yqbjtu.blog.163.com/blog/static/529426201672674444341/
第一步显示定义XSD,这个参考网上很多教程,本文给出一个简单实例。
order_v1.xsd
<?xml version="1.0" encoding="UTF-8"?><!--- Licensed Text--><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://demo.com/OrderXMLSchema_v1" xmlns:tns="http://demo.com/OrderXMLSchema_v1"><xsd:element name="shipOrder" type="tns:shipOrder"></xsd:element> <xsd:complexType name="shipOrder"> <xsd:sequence maxOccurs="1" minOccurs="1"> <xsd:element name="orderperson" type="xsd:string" minOccurs="1" maxOccurs="1"></xsd:element> <xsd:element name="order-description" type="xsd:string" minOccurs="1" maxOccurs="1"></xsd:element> <xsd:element name="items" type="tns:items" maxOccurs="1" minOccurs="1"></xsd:element> <xsd:element name="item-2" type="tns:item-2" maxOccurs="unbounded" minOccurs="1"></xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="items"> <xsd:sequence maxOccurs="unbounded" minOccurs="1"> <xsd:element name="item" minOccurs="1" maxOccurs="1"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="index" type="xsd:unsignedLong" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="item-2"> <xsd:sequence maxOccurs="1" minOccurs="1"> <xsd:element name="title" type="xsd:string"/> <xsd:element name="note" type="xsd:string" minOccurs="0"/> <xsd:element name="quantity" type="xsd:positiveInteger"/> <xsd:element name="price" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="index" type="xsd:unsignedLong" /> </xsd:complexType></xsd:schema>
Xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--- Licensed Description.--><shipOrder xmlns="http://demo.com/OrderXMLSchema_v1" xmlns:server="http://demo.com/OrderXMLSchema_v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <orderperson>John Smith</orderperson> <order-description>This document is used for demo sxN#</order-description> <items> <item index="1"> some potatoes </item> <item index="2"> some tomatoes </item> </items> <item-2 index="1"> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item-2> <item-2 index="2"> <title>Hide your heart</title> <note>this is for testing note, it is not required</note> <quantity>1</quantity> <price>9.90</price> </item-2></shipOrder>
最后是我们的java代码
import java.io.File;import java.io.UnsupportedEncodingException;import javax.xml.transform.Source;import javax.xml.transform.stream.StreamSource;import javax.xml.validation.Schema;import javax.xml.validation.SchemaFactory;import javax.xml.validation.Validator;import org.xml.sax.SAXException;public class index="2"> <title>Hide your heart</title> <note>this is for testing note, it is not required</note> <quantity>1</quantity> <price>9.90</price> </item-2>
其余部分基本类似,比较需要注意的是item类型,它有attribute也有value。
<item index="2"> some tomatoes </item>
它的定义是
<xsd:element name="item" minOccurs="1" maxOccurs="1"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="index" type="xsd:unsignedLong" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element>
图片
1 0
- 使用xsd验证xml
- 使用XSD验证xml
- 使用xsd文件验证xml
- 使用架构(XSD)验证XML文件
- java中使用xsd验证xml
- C#使用XSD来验证XML。
- 使用xsd验证xml文件是否规范
- Xsd 验证xml--C#
- XSD验证XML
- XSD 验证 xml 合法性
- [代码]使用XSD验证XML示例1(LINQ to XML)
- [代码]使用XSD验证XML示例2(LINQ to XML)
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- VB Script 如何使用XSD验证XML文档格式
- XSD 验证xml例子 --C#
- 验证xml--xsd和dtd
- .NET 用XSD验证XML
- C# 利用Xsd验证xml
- 解决framework.jar push 到/system/framework/中不生效
- 51nod B君的圆锥
- Struts2中的ModelDriven机制及其运用
- CentOS安装软件时提示找不到镜像:Couldn’t resolve host
- 编程的极易忽视的错误
- 使用XSD验证xml
- GSON谷歌方法之:Json解析
- 标签栏主界面的实现
- POJ 2188线段树求逆序对
- 汉诺塔递归算法
- Cannot nest 'PRJ/src/main/resource' inside 'PRJ/src'. To enable the nesting exclude 'main/' from 'PR
- 如何快速了解一个系统
- cocos2d-x(3): 创建精灵基础学习
- 关于自己android学习中的RecycleView的问题