posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2008年3月>
          2425262728291
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 865113
          • 排名 - 44

          最新評論

          閱讀排行榜

          SchemaValidation .java

          package com.founder.xml;

          import Java.io.IOException;
          import Java.io.InputStream;

          import javax.xml.XMLConstants;
          import javax.xml.parsers.ParserConfigurationException;
          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 SchemaValidation {

           /**
            * @param args
            * @throws IOException
            * @throws SAXException
            * @throws ParserConfigurationException
            */
           public static void main(String[] args) throws ParserConfigurationException,
             SAXException, IOException {
            SchemaValidation sv = new SchemaValidation();
            sv.validate();
           }

           public void validate() {
            try {
             String strLang = XMLConstants.W3C_XML_SCHEMA_NS_URI;
             SchemaFactory factory = SchemaFactory.newInstance(strLang);

             InputStream isSchema = getClass().getResourceAsStream("/founder.xsd");
             StreamSource ss = new StreamSource(isSchema);
             Schema schema = factory.newSchema(ss);

             Validator validator = schema.newValidator();
             
             InputStream isXML = getClass().getResourceAsStream("/founder.xml");
             
             StreamSource source = new StreamSource(isXML);
             validator.validate(source);
             System.out.println("Result : Valid!");

            } catch (Exception e) {
             e.printStackTrace();
             System.out.println("Result : Invalid!");
            }

           }

          }


          founder.xsd

          <?xml version="1.0"?>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://www.founder.com" elementFormDefault="qualified">
           <xs:element name="note">
            <xs:complexType>
             <xs:sequence>
              <xs:element name="to" type="xs:string" />
              <xs:element name="from" type="xs:string" />
              <xs:element name="heading" type="xs:string" />
              <xs:element name="body" type="xs:string" />
             </xs:sequence>
            </xs:complexType>
           </xs:element>
          </xs:schema>


          founder.xml

          <?xml version="1.0"?>
          <note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.founder.com founder.xsd">
           <to>Tove</to>
           <from>Jani</from>
           <heading>Reminder</heading>
           <body>Don't forget me this weekend!</body>
          </note>



          posted on 2008-03-27 21:46 周銳 閱讀(2258) 評論(1)  編輯  收藏 所屬分類: JavaXML
          主站蜘蛛池模板: 宁河县| 甘孜| 泰来县| 岗巴县| 汉寿县| 双辽市| 广饶县| 彭州市| 海淀区| 丽水市| 乐昌市| 越西县| 常德市| 阜南县| 五华县| 淮滨县| 莆田市| 仙桃市| 随州市| 永春县| 涿鹿县| 图木舒克市| 昭平县| 青浦区| 大关县| 遂川县| 榆林市| 常山县| 密云县| 天台县| 平武县| 巴彦淖尔市| 阿拉尔市| 凯里市| 崇礼县| 高密市| 小金县| 吐鲁番市| 苏尼特左旗| 郧西县| 延津县|