xml與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); } } }
不拋例外表示偵測通過。
以上是XSD與XML的偵測範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!