Last active
December 20, 2015 18:18
-
-
Save newgeekorder/6174549 to your computer and use it in GitHub Desktop.
Groovy xsd valdiation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.XMLConstants | |
import javax.xml.transform.stream.StreamSource | |
import javax.xml.validation.SchemaFactory | |
class jasonXsdValdation { | |
static def xml = """ | |
<?xml version="1.0" encoding="UTF-8"?> | |
<jason> | |
<date>20133-08-05T12:37:36.3970235Z</date> | |
</jason> | |
""".trim() | |
static def xsd = """ | |
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> | |
<xs:element name="jason"> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element name="date" type=" xs:dateTime"/> | |
</xs:sequence> | |
</xs:complexType> | |
</xs:element> | |
</xs:schema> | |
""".trim() | |
static main(args) { | |
def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) | |
def schema = factory.newSchema(new StreamSource(new StringReader(xsd))) | |
def validator = schema.newValidator() | |
validator.validate(new StreamSource(new StringReader(xml))) | |
print "validated" | |
} | |
}import javax.xml.XMLConstants | |
import javax.xml.transform.stream.StreamSource | |
import javax.xml.validation.SchemaFactory | |
class jasonXsdValdation { | |
static def xml = """ | |
<?xml version="1.0" encoding="UTF-8"?> | |
<jason> | |
<date>20133-08-05T12:37:36.3970235Z</date> | |
</jason> | |
""".trim() | |
static def xsd = """ | |
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> | |
<xs:element name="jason"> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element name="date" type=" xs:dateTime"/> | |
</xs:sequence> | |
</xs:complexType> | |
</xs:element> | |
</xs:schema> | |
""".trim() | |
static main(args) { | |
def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) | |
def schema = factory.newSchema(new StreamSource(new StringReader(xsd))) | |
def validator = schema.newValidator() | |
validator.validate(new StreamSource(new StringReader(xml))) | |
print "validated" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment