Home > Backend Development > XML/RSS Tutorial > XSD and XML detection sample code

XSD and XML detection sample code

黄舟
Release: 2017-04-15 11:17:11
Original
2082 people have browsed it

Example of matching detection between xml and xsd

import java.io.File;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;

public class XmlHandler {

	public static final String XSD_FILE_PATH = "automation.xsd";
    public static void checkXSD(File xmlFile)  {
     
    	SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    	try {
    		schemaFactory.newSchema(
                    new Source[] {
                   		 new StreamSource(
                   				 Thread.currentThread().getContextClassLoader().getResourceAsStream(XSD_FILE_PATH))}
                    ).newValidator().validate(new StreamSource(xmlFile));
		} catch (Exception e) {
			throw new RuntimeException("XSD检测失败",e);
		}
			
    }
}
Copy after login

If no exception is thrown, the detection is passed.

The above is the detailed content of XSD and XML detection sample code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template