0

I have the following xsd file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="data">
        <xs:complexType>
            <xs:sequence>
                <xs:element type="xs:string" name="city"/>
                <xs:element name="temperature">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="value">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:byte">
                                            <xs:attribute type="xs:string" name="unit"/>
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element type="xs:dateTime" name="measured_at_ts"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

And xml file:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <city>Copenhagen</city>
    <temperature>
        <value unit="celsius">27</value>
    </temperature>
    <measured_at_ts>2020-08-01T18:00:00</measured_at_ts>
</data

When I try to execute the following code:

    public static void main(String[] args) {
        System.out.println(XmlHandler.isXmlValid("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<data>" +
                "    <city>Copenhagen</city>" +
                "    <temperature>" +
                "        <value unit=\"celsius\">27</value>" +
                "    </temperature>" +
                "    <measured_at_ts>2020-08-01T18:00:00</measured_at_ts>" +
                "</data>"));

    private static boolean isXmlValid(String xml) {
        boolean xmlValid = true;
        try {
            validate(xml);
        } catch (SAXException | IOException | ParserConfigurationException e) {
            xmlValid = false;
            e.printStackTrace();
        }
        return xmlValid;
    }

    private static void validate(String xml) throws SAXException, ParserConfigurationException, IOException {
        final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        final Document document = builder.parse(new InputSource(new StringReader(xml)));
        final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final String s = XmlHandler.class.getResource("/").getPath() + "weather.xsd";
        factory.newSchema(new File(s)).newValidator().validate(new DOMSource(document));
    }

I get org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element 'data'.

I have tried boiling down the example to these:

xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="data">
    </xs:element>
</xs:schema>

xml:

<?xml version=\"1.0\" encoding=\"UTF-8\"?><data></data>

and still get the same result.

Any pointers as to what causes the aforementioned error would be appreciated!

zaxme
  • 1,065
  • 11
  • 29
  • I am not able to recreate this error - your code works OK for me (It prints `true`). If you print your `String s`, is it as expected? I assume yes, but maybe worth checking? – andrewJames Sep 06 '20 at 14:02
  • One minor note: The `factory.newSchema()` method takes a URL - so you don't need to get the XSD resource and convert it to a file. You can simplify the code slightly: `final URL xsdUrl = XmlHandler.class.getResource(...);` and `factory.newSchema(xsdUrl)...` (but again, taking care that you are picking up the expected resource, of course). – andrewJames Sep 06 '20 at 14:05
  • @andrewjames I did verify I have the right file path. Still I'm getting the same error. Can you share a gist of the the working code you have? – zaxme Sep 06 '20 at 14:42
  • Sure thing: [Here is a repo](https://github.com/northcoder-repo/XmlValidationDemo) of the 3 relevant files, to show full details. Hope it helps (not sure it will, though). Note the POM: Java 14 and build sections to copy the XSD file, & to make an executable JAR. – andrewJames Sep 06 '20 at 15:58
  • Does this answer your question? [XML Schema Validation : Cannot find the declaration of element](https://stackoverflow.com/questions/15642046/xml-schema-validation-cannot-find-the-declaration-of-element) – Lakshan Sep 07 '20 at 03:13
  • Thanks for the suggestion but no. I have no namespaces in my XML and XSD declaration. – zaxme Sep 07 '20 at 06:54
  • @andrewjames I checked out your code and it was working. When I copied the exact files over to my project - it was failing. In the end I used - https://stackoverflow.com/a/6362975/1450817 to get what I wanted. Thank you for the help! – zaxme Sep 07 '20 at 07:47

0 Answers0