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.
          公告本博客在此聲明部分文章為轉(zhuǎn)摘,只做資料收集使用。


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

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 863200
          • 排名 - 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 周銳 閱讀(2253) 評論(1)  編輯  收藏 所屬分類: JavaXML
          主站蜘蛛池模板: 北安市| 大化| 乌恰县| 望城县| 茌平县| 雅安市| 治多县| 额济纳旗| 镇坪县| 湘西| 昭通市| 广宁县| 济南市| 汉川市| 化州市| 东台市| 西青区| 德保县| 宁德市| 民丰县| 得荣县| 全州县| 大城县| 综艺| 宜昌市| 积石山| 莱西市| 盐山县| 桂阳县| 合阳县| 永济市| 南雄市| 咸阳市| 扎赉特旗| 乐平市| 龙南县| 铜鼓县| 永福县| 衡山县| 葵青区| 西乌|