Note that there are some explanatory texts on larger screens.

plurals
  1. POtroubles validating XML against XSD (java)
    text
    copied!<p>I'm trying to write a unit test that will validate an XML string document against an XSD. Note that the XSDs are stored on disk and the URLs used for the namespaces in the XML doc are not actually available via a web server.</p> <p>Here is the code:</p> <pre><code> @Test public void testValidateAgainstXSD() throws Exception { String xmlDoc = MY_XML_DOC_SAMPLE; File schemaFile = new File("/Users/philswenson/dev/optimize_l/modules/ae/staging/eda-eventtypes/Analysis/1.0/MeasurementResultStatistics.xsd"); Source xmlFile = new StreamSource(xmlDoc); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(schemaFile); Validator validator = schema.newValidator(); try { validator.validate(xmlFile); System.out.println(xmlFile.getSystemId() + " is valid"); } catch (SAXException e) { throw new RuntimeException(e); } } </code></pre> <p>When I run the test I get the error below. Any ideas on what I'm doing wrong?</p> <pre><code>java.net.MalformedURLException: no protocol: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;p:MeasurementResultStatistics xmlns:p="http://namespaces.myco.com/EDA/Analysis/1.0"&gt; &lt;p:Average&gt;5.0&lt;/p:Average&gt; &lt;p:Minimum&gt;0.1&lt;/p:Minimum&gt; &lt;p:Maximum&gt;10.3&lt;/p:Maximum&gt; &lt;p:StandardDeviation&gt;0.0&lt;/p:StandardDeviation&gt; &lt;p:HourOfDay&gt;7&lt;/p:HourOfDay&gt; &lt;p:DayOfWeek&gt;Mon&lt;/p:DayOfWeek&gt; &lt;p2:MeasurementDefinition xmlns:p2="http://namespaces.myco.com/EDA/Analysis/1.0"&gt; &lt;p2:Name&gt;TEST KPI NAME&lt;/p2:Name&gt; &lt;p2:DisplayName&gt;TEST DISPLAY NAME&lt;/p2:DisplayName&gt; &lt;p2:Version&gt;1&lt;/p2:Version&gt; &lt;p2:MeasurementUnits&gt;TEST UOM&lt;/p2:MeasurementUnits&gt; &lt;p2:TimeInterval&gt;10000&lt;/p2:TimeInterval&gt; &lt;/p2:MeasurementDefinition&gt; &lt;p3:MeasurementMember xmlns:p3="http://namespaces.myco.com/EDA/Analysis/1.0"&gt; &lt;p3:Name&gt;TEST MONITOR STRING ID&lt;/p3:Name&gt; &lt;p3:DisplayName&gt;TEST DISPLAY NAME&lt;/p3:DisplayName&gt; &lt;/p3:MeasurementMember&gt; &lt;/p:MeasurementResultStatistics&gt; at java.net.URL.&lt;init&gt;(URL.java:567) at java.net.URL.&lt;init&gt;(URL.java:464) at java.net.URL.&lt;init&gt;(URL.java:413) at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source) at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.jaxp.validation.StreamValidatorHelper.validate(Unknown Source) at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source) at javax.xml.validation.Validator.validate(Validator.java:127) at com.myco.optimize.monitor.engine.XSDValidatorTest.testValidateAgainstXSD(XSDValidatorTest.java:46) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload