Question

This is java class file.

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
import java.io.*;

public class Jaxp_1
{
public static void main(String [] args) throws Exception
{
Source schemaFile = new StreamSource(new File("xsd/img.xsd"));
Source xmlFile = new StreamSource(new File("xml/imgone.xml"));

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");
        System.out.println();
    }
    catch (SAXException e) 
    {
        System.out.println(schemaFile.getSystemId() + " is NOT valid");
        System.out.println("Reason: " + e.getMessage());
    }
 }
 }

This is xml file.

<?xml version="1.0" encoding="UTF-8"?>
<edge xmlns="http://www.example.org/img">
<image x="143.05" y="2" height="66" width="537"
    xhref="/dccp_repository/dam/other/images/insurance.jpg" id="Image_48"
    isLocked="false" rx="143.05" ry="2" rotation="0" />
</edge>

this is XSD file.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/img"
xmlns:tns="http://www.example.org/img" elementFormDefault="qualified">
<element name="edge">
    <complexType>
        <sequence>
            <element name="image">
                <complexType>
                    <attribute name="x" type="int"></attribute>
                    <attribute name="y" type="int"></attribute>
                    <attribute name="height" type="int"></attribute>
                    <attribute name="width" type="int"></attribute>
                    <attribute name="xhref" type="string"></attribute>
                    <attribute name="id" type="string"></attribute>
                    <attribute name="isLocked" type="string"></attribute>
                    <attribute name="rx" type="double"></attribute>
                    <attribute name="ry" type="int"></attribute>
                    <attribute name="rotation" type="int"></attribute>
                </complexType>
            </element>
        </sequence>
    </complexType>
</element>
</schema>

I changed type of attribute like attribute name="x" type="int". i got error like :

file:/D:/Maheshkumar.V/workspace/JavaOne/JavaIO/xsd/img.xsd is NOT valid Reason: cvc-datatype-valid.1.2.1: '143.05' is not a valid value for 'integer'.

it won't show element name in exception . where is integer ? it don't specific 'image' element.

if i had large xml. how can i identify error?

Was it helpful?

Solution

The exception that is thrown is a SAXParseException, a subclass of SAXException. SAXException cannot tell you where the fault occurred, but SAXParseException can, through getLineNumber() and getColumnNumber(). These do not name the element at fault but allow you to identify it through its location in the xml file. The line and column number point to the closing tag for the element.

You could use this:

try{
    validator.validate(xmlFile);
    System.out.println(xmlFile.getSystemId()+ " is valid");
    System.out.println();
} catch (SAXParseException e) 
{
    System.out.println(schemaFile.getSystemId() + " is NOT valid");
    System.out.println("Reason: " + e.getMessage() 
         + " at line:" + e.getLineNumber() 
         + " at column:" + e.getColumnNumber() +".");
} catch (SAXException e) 
{
    System.out.println(schemaFile.getSystemId() + " is NOT valid");
    System.out.println("Reason: " + e.getMessage());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top