How to validate an XML file in Java?

by weldon_huels , in category: Java , a year ago

How to validate an XML file in Java?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by hortense_donnelly , a year ago

@weldon_huels To validate an XML file in Java, you can use the javax.xml.validation package, which provides an API for parsing and validating XML documents.


Here's an example of how you can use the javax.xml.validation package to validate an XML file against a schema:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import java.io.File;

public class XMLValidator {

    public static void main(String[] args) throws Exception {
        // Create a SchemaFactory capable of understanding WXS schemas.
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        // Load the schema file.
        File schemaLocation = new File("schema.xsd");
        Schema schema = factory.newSchema(schemaLocation);

        // Create a Validator object, which can be used to validate an instance document.
        Validator validator = schema.newValidator();

        // Validate the XML file.
        File xmlFile = new File("document.xml");
        validator.validate(new StreamSource(xmlFile));

        System.out.println(xmlFile.getName() + " is valid.");
    }
}


In this example, the schema is specified using a file (schema.xsd), but you can also use a StreamSource or a Source object to specify the schema. The XML file to be validated (document.xml) is also specified using a File object, but you can also use a StreamSource or a Source object to specify the XML file.


If the XML file is valid according to the schema, the validate method will return without throwing an exception. If the XML file is not valid, the validate method will throw a SAXException.

by hershel.jaskolski , 5 months ago

@weldon_huels 

To validate an XML file against a schema in Java, you can use the javax.xml.validation package, which provides an API for parsing and validating XML documents. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;

public class XMLValidator {

    public static void main(String[] args) {
        try {
            // Create a SchemaFactory instance with the W3C XML schema namespace
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            // Load and compile the schema file
            File schemaFile = new File("schema.xsd");
            Schema schema = schemaFactory.newSchema(schemaFile);

            // Create a Validator object from the schema
            Validator validator = schema.newValidator();

            // Load the XML file to be validated
            File xmlFile = new File("document.xml");
            Source xmlSource = new StreamSource(xmlFile);

            // Validate the XML against the schema
            validator.validate(xmlSource);

            System.out.println("XML is valid.");

        } catch (SAXException e) {
            System.out.println("XML is not valid: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Error reading XML file: " + e.getMessage());
        }
    }
}


In this example, we first create a SchemaFactory instance with the XMLConstants.W3C_XML_SCHEMA_NS_URI constant, which represents the W3C XML schema namespace. Then, we load and compile the schema file using the SchemaFactory.newSchema method. Next, we create a Validator object from the schema.


To validate an XML file, we load the XML file using a StreamSource and then call the Validator.validate method, passing in the XML source. If the XML file is valid according to the schema, the validate method will return normally. If the XML file is not valid, a SAXException will be thrown, and you can catch it to handle the validation error.