Question

How do I reference xml schema using JDK 1.4.x?

I can't use this code as an example (origin)

public static void main(String[] args) {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
        Document document = parser.parse(new File("NewFile.xml"));

        Schema schema = schemaFactory.newSchema(new File("AccountList.xsd"));
        Validator validator = schema.newValidator();

        validator.validate(new DOMSource(document));
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

because there are no packages like

import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

they appeared later in JDK 1.5.x.

Was it helpful?

Solution

The schema validation API is part of JAXP 1.3. You can download a version of JAXP 1.3 that works with JDK 1.4 here:

https://jaxp.java.net/1.3/

At least you can in theory. It's many years since I tried it, before the Oracle takeover, and it was awkward then, but it was not impossible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top