Skip to content

Instantly share code, notes, and snippets.

@deepakshrma
Created June 11, 2022 11:05
Show Gist options
  • Save deepakshrma/2128edf20faabbb726be728d200eb8c5 to your computer and use it in GitHub Desktop.
Save deepakshrma/2128edf20faabbb726be728d200eb8c5 to your computer and use it in GitHub Desktop.
XSD Parser Kotlin
import org.w3c.dom.Document
import org.xml.sax.SAXException
import org.xml.sax.SAXParseException
import org.xml.sax.helpers.DefaultHandler
import java.io.StringReader
import javax.xml.XMLConstants
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory
//private fun setFeature(parser: DOMParser, feature: String, setting: Boolean) {
// parser.setFeature(feature, setting)
//}
fun Document.getTextByName(name: String) = this.getElementsByTagName(name)?.item(0)?.textContent
fun main(args: Array<String>) {
val sources: Array<StreamSource> = arrayOf(StreamSource(StringReader(xsd)))
val factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
val schema = factory.newSchema(sources)
val dbf = DocumentBuilderFactory.newInstance()
dbf.isNamespaceAware = true
dbf.schema = schema
val db = dbf.newDocumentBuilder()
db.setErrorHandler(SAXParserErrorHandler1())
try {
val doc = db.parse(xml.byteInputStream())
println(doc.getTextByName("checksum"))
} catch (e: SAXException) {
println(e)
}
}
class SAXParserErrorHandler1 : DefaultHandler() {
override fun warning(ex: SAXParseException) {
error(ex)
}
override fun error(ex: SAXParseException) {
throw ex
}
override fun fatalError(ex: SAXParseException) {
error(ex)
}
}
val xml = """<?xml version="1.0"?>
<quotations
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<quote quoteid="A42">
<saying>
I love deadlines. I like the whooshing sound
they make as they fly by.
</saying>
<attribution>Douglas Adams</attribution>
<era>Modern</era>
<checksum>1111111</checksum>
</quote>
<quote quoteid="H3">
<saying>
My goal is simple. It is complete understanding
of the universe, why it is as it is and why
it exists at all.
</saying>
<attribution>Stephen Hawking</attribution>
<era>Modern</era>
</quote>
<quote quoteid="U1">
<saying>
Some people make things happen, somße watch while
things happen, and some wonder "What happened?"
</saying>
<attribution>Unknown</attribution>
<era>Modern!@</era>
</quote>
</quotations>"""
val xsd = """<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="quotations">
<xs:complexType>
<xs:sequence>
<xs:element name="quote" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="saying" type="xs:string"/>
<xs:element name="attribution" type="xs:string"/>
<xs:element name="era" type="String"/>
<xs:element name="checksum" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="quoteid" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="String">
<xs:restriction base="xs:string">
<xs:pattern value="[\w@]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>"""
//""" <dependency>
// <groupId>xerces</groupId>
// <artifactId>xercesImpl</artifactId>
// <version>2.12.2</version>
// </dependency>"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment