简体   繁体   English

从Java中的zip对XML解析XML

[英]Parsing xml against xsd from a zip in Java

I have a .zip file that contains .xml and .xsd file. 我有一个包含.xml和.xsd文件的.zip文件。

I'm reading it with a code similar to this one . 我有类似的代码,阅读它这一个 Now, when I try to parse the .xml I get an error that schema is not found. 现在,当我尝试解析.xml时,出现一个错误,即找不到架构。 How do I load the .xsd beforehand and then parse the .xml using that? 如何预先加载.xsd,然后使用该解析.xml?

  1. Get the ZipInputStream from the ZipFile - Reading text files in a zip archive 从ZipFile获取ZipInputStream- 读取zip存档中的文本文件

  2. Use javax.xml.transform.stream.StreamSource - http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html 使用javax.xml.transform.stream.StreamSource- http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new StreamSource(xsdZipInputStream)); 架构架构= sf.newSchema(new StreamSource(xsdZipInputStream)); Validator validator = schema.newValidator(); 验证程序validator = schema.newValidator(); validator.validate(xml); validateator.validate(xml);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM