-
-
Save natebass/dd5f8851095b38b948d7 to your computer and use it in GitHub Desktop.
Exemplo de validação de Schema XML usando api padrão do Java. Se o XML estiver OK, nada ocorre. Se estiver errado, será lançada uma org.xml.sax.SAXParseException.
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
<?xml version="1.0" encoding="UTF-8"?> | |
<pedido id="1" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="pedido.xsd"> | |
<cliente>João</cliente> | |
<destino> | |
<nomekkk>Maria</nomekkk> | |
<endereco>SCS 1</endereco> | |
<cidade>Brasília</cidade> | |
</destino> | |
<item> | |
<produto>Apostila Java</produto> | |
<quantidade>1</quantidade> | |
<preco>10.90</preco> | |
</item> | |
<item> | |
<produto>Apostila Javascript</produto> | |
<quantidade>1</quantidade> | |
<preco>9.90</preco> | |
</item> | |
</pedido> |
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
<?xml version="1.0" encoding="UTF-8" ?> | |
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> | |
<xs:element name="pedido"> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element name="cliente" type="xs:string"/> | |
<xs:element name="destino"> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element name="nome" type="xs:string"/> | |
<xs:element name="endereco" type="xs:string"/> | |
<xs:element name="cidade" type="xs:string"/> | |
</xs:sequence> | |
</xs:complexType> | |
</xs:element> | |
<xs:element name="item" maxOccurs="unbounded"> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element name="produto" type="xs:string"/> | |
<xs:element name="quantidade" type="xs:positiveInteger"/> | |
<xs:element name="preco" type="xs:decimal"/> | |
</xs:sequence> | |
</xs:complexType> | |
</xs:element> | |
</xs:sequence> | |
<xs:attribute name="id" type="xs:string" use="required"/> | |
</xs:complexType> | |
</xs:element> | |
</xs:schema> |
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
package validador; | |
import java.io.File; | |
public class Teste { | |
public static void main(String[] args) throws Exception { | |
Validador validador = new Validador(); | |
validador.valida(new File("pedido.xml"), new File("pedido.xsd")); | |
} | |
} |
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
package validador; | |
import java.io.File; | |
import javax.xml.XMLConstants; | |
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; | |
public class Validador { | |
public void valida(File xml, File xsd) throws Exception{ | |
Source schemaFile = new StreamSource(xsd); | |
Source xmlFile = new StreamSource(xml); | |
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
Schema schema = schemaFactory.newSchema(schemaFile); | |
Validator validator = schema.newValidator(); | |
validator.validate(xmlFile); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment