zoukankan      html  css  js  c++  java
  • Java

    package com.huey.dream.utils;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    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 XSDValidator {
        
        static public void validate(InputStream xsdStream, InputStream xmlStream) throws SAXException, IOException {
            SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");                
            Source xsdSource = new StreamSource(xsdStream);
            Schema schema = schemaFactory.newSchema(xsdSource);
            
            Source xmlSource = new StreamSource(xmlStream);
            
            Validator validator = schema.newValidator();
            validator.validate(xmlSource);        
        }
        
    }

    测试。

    package com.huey.dream;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.junit.Test;
    import org.xml.sax.SAXException;
    
    import com.huey.dream.utils.XSDValidator;
    
    public class XSDValidatorTest {
    
        @Test
        public void testValidate() throws Exception {
            String xsdPath = "files/Books.xsd";
            String xmlPath = "files/Books.xml";
            InputStream xsdStream = null;
            InputStream xmlStream = null;
            
            try {
                xsdStream = new FileInputStream(xsdPath);
                xmlStream = new FileInputStream(xmlPath);
                XSDValidator.validate(xsdStream, xmlStream);
                System.out.println("Validate successfully.");
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (xsdStream != null) {
                    xsdStream.close();
                }
                if (xmlStream != null) {
                    xmlStream.close();
                }
            }
        }
    
    }
    XSDValidatorTest.java
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
        <xs:element name="books">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="book">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="title" type="xs:string"/>
                                <xs:element name="authors">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="author" maxOccurs="unbounded">
                                                <xs:complexType>
                                                    <xs:sequence>
                                                        <xs:element name="firstName" type="xs:string"/>
                                                        <xs:element name="lastName" type="xs:string"/>
                                                        <xs:element name="nationality" type="xs:string" minOccurs="0"/>
                                                    </xs:sequence>
                                                </xs:complexType>
                                            </xs:element>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                                <xs:element name="publisher" type="xs:string" minOccurs="0"/>
                                <xs:element name="publishDate" type="xs:date" minOccurs="0"/>
                                <xs:element name="isbn" type="xs:string"/>
                                <xs:element name="price" type="xs:double" minOccurs="0"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    Books.xsd
    <?xml version="1.0" encoding="UTF-8"?>
    <books>
        <book>
            <title>HTTP权威指南</title>
            <authors>
                <author>
                    <firstName>David</firstName>
                    <lastName>Gourley</lastName>
                </author>
                <author>
                    <firstName>Brian</firstName>
                    <lastName>Totty</lastName>
                </author>
            </authors>
            <publisher>人民邮电出版社</publisher>
            <publishDate>2012-09-01</publishDate>
            <isbn>9787115281487</isbn>
            <price>109.00</price>
        </book>
    </books>
    Books.xml
  • 相关阅读:
    1062 Talent and Virtue (25 分)
    1083 List Grades (25 分)
    1149 Dangerous Goods Packaging (25 分)
    1121 Damn Single (25 分)
    1120 Friend Numbers (20 分)
    1084 Broken Keyboard (20 分)
    1092 To Buy or Not to Buy (20 分)
    数组与链表
    二叉树
    时间复杂度与空间复杂度
  • 原文地址:https://www.cnblogs.com/huey/p/4600817.html
Copyright © 2011-2022 走看看