Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

Validator tài liệu XML

Kiểm tra tính hợp lệ của file XML Dùng DOM

package validationex;

import javax.xml.XMLConstants;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Source;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamSource;

import javax.xml.validation.Schema;

import javax.xml.validation.SchemaFactory;

import javax.xml.validation.Validator;

import org.w3c.dom.Document;

public class DOMValidator {

public static void main (String[] args) {

String xmlfile = “files/tomcatusers.xml”;

String xsdfile = “files/tomcatusers.xsd”;

try{

DocumentBuilder parser =

DocumentBuilderFactory.newInstance ().newDocumentBuilder ();

Document doc = parser.parse (xmlfile);

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Source schemaFile = new StreamSource(xsdfile);

Schema schema = factory.newSchema (schemaFile);

Validator validator = schema.newValidator ();

validator.validate (new DOMSource(doc));

System.out.println(xmlfile + ” document is valid!”);

}catch(Exception e){

System.out.println(xmlfile + ” document is NOT valid!”);

System.out.println(e.getMessage());

}

}

}

Kiểm tra tính hợp lệ của file XML dung SAX

package validationex;

import java.io.File;

import javax.xml.XMLConstants;

import javax.xml.transform.Source;

import javax.xml.transform.sax.SAXSource;

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.InputSource;

public class SAXValidator {

public static void main (String[] args) {

String xmlfile = “files/tomcatusers.xml”;

String xsdfile = “files/tomcatusers.xsd”;

try{

InputSource is = new InputSource(xmlfile);

SchemaFactory factory =

SchemaFactory.newInstance (XMLConstants.W3C_XML_SCHEMA_NS_URI);

Source schemaFile = new StreamSource(new File(xsdfile));

Schema schema = factory.newSchema (schemaFile);

Validator validator = schema.newValidator ();

validator.validate (new SAXSource(is));

System.out.println(xmlfile + ” document is valid!!!!”);

}catch(Exception e){

System.out.println(xmlfile + ” document is NOT valid!”);

System.out.println(e.getMessage());

}

}

}

Với file source: tomcatusers.xsd

<?xml version=”1.0″ encoding=”utf-8″?>

<xs:schema attributeFormDefault=”unqualified”

elementFormDefault=”qualified” xmlns:xs=”http://www.w3.org/2001/XMLSchema”&gt;

<xs:element name=”tomcat-users”>

<xs:complexType>

<xs:sequence>

<xs:element maxOccurs=”unbounded” name=”role”>

<xs:complexType>

<xs:attribute name=”rolename” type=”xs:string” use=”required” />

</xs:complexType>

</xs:element>

<xs:element maxOccurs=”unbounded” name=”user”>

<xs:complexType>

<xs:attribute name=”username” type=”xs:string” use=”required” />

<xs:attribute name=”password” type=”xs:string” use=”required” />

<xs:attribute name=”roles” type=”xs:string” use=”required” />

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

File tomcatusers.xml

<?xml version=“1.0” encoding=“utf-8” standalone=“no”?>

<tomcat-users>

<role rolename=“tomcat”/>

<role rolename=“role1”/>

<role rolename=“manager”/>

<role rolename=“admin”/>

<user password=“tomcat” roles=“tomcat” username=“tomcat”/>

<user password=“tomcat” roles=“tomcat,role1” username=“both”/>

<user password=“tomcat” roles=“role1” username=“role1”/>

<user password=“” roles=“admin,manager” username=“admin”/>

<user password=“s” roles=“admin,manager” username=“teo”/>

</tomcat-users>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: