Michael,

In ErrorHandler, I tried to implement to query the property but at run time it 
gave me belwo error. It looks like that I am querying the wrong property. I 
tried to find the appropriate property but could not .

What property should I use so that I can get the current element node when 
validator throws error ?  Please find the attached file for reference .

Progrma  started
instance error org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid 
content was found starting with element 'Doc:Id1'. One of 
'{"urn:swift:xsd:swift.eni$camt.028.001.02":Id}' is expected.
org.xml.sax.SAXNotRecognizedException: 
http://apache.org/xml/properties/dom/current-element-node
Program ended


Thanks 
ajay.


--- On Sun, 11/9/08, Michael Glavassevich <[EMAIL PROTECTED]> wrote:

> From: Michael Glavassevich <[EMAIL PROTECTED]>
> Subject: Re: how to xpath when schema validation fails.
> To: j-users@xerces.apache.org
> Cc: [EMAIL PROTECTED], [EMAIL PROTECTED]
> Date: Sunday, November 9, 2008, 2:45 AM
> Hi Ajay,
> 
> ajay bhadauria <[EMAIL PROTECTED]> wrote on
> 11/07/2008 03:17:21 PM:
> 
> > Michael,
> >
> > Thanks for response and explanation.
> >
> > But can we use current-element-node property which
> could be set to
> > javax.xml.validation.Validator using setProperty and
> we get the node
> > which is in error during validation?
> 
> The current element node is the current element being
> visited by the
> validator. It's not necessarily where the error is
> located though often is.
> I suggested querying this property in your error handler
> because it's the
> best approximation available from the validator at that
> point.
> 
> > Once we get the node and then with getParaentNode
> recursively
> > and get the complete xpath ?
> 
> That will give you an XPath for the current element node,
> but not
> necessarily an XPath which points to the location of the
> error which was
> reported. This certainly never works for attributes.
> 
> > Regards
> > Ajay
> 
> Thanks.
> 
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: [EMAIL PROTECTED]
> E-mail: [EMAIL PROTECTED]


      
import java.io.*;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.Validator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.transform.dom.DOMSource;

public class TestCurrentNode implements ErrorHandler
{

        DocumentBuilderFactory dbf = null;
        Schema schema = null;
        Validator validator = null;
        public Document parseDom(File xmlFile, String xsdFile)
        {
              String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
              SchemaFactory factory = SchemaFactory.newInstance(language);
              try 
              {
              schema = factory.newSchema(new File(xsdFile));
              }
              catch ( Exception e )
              {

                      System.out.println(e.toString());
              }
              validator = schema.newValidator();
              validator.setErrorHandler(this);
              Document document= null;
              DocumentBuilderFactory dFactory = 
DocumentBuilderFactory.newInstance();
              dFactory.setNamespaceAware(true);
              DocumentBuilder dBuilder =null;
              try
              {
              dBuilder = dFactory.newDocumentBuilder();
              }
              catch( Exception e)
              {
                   System.out.println(e.toString());
              }

              try
              {
                    document = dBuilder.parse( new FileInputStream(xmlFile));
              }
              catch (SAXException e)
              {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
              }
              catch (IOException e)
              {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
              }
              DOMSource source = new DOMSource(document);
              try
              {
              validator.validate(source); 
              }
              catch (Exception e)
              {
                 System.out.println(e.toString());

              }
              return null;

        }

        public void warning(SAXParseException arg0) throws SAXException
        {
                System.out.println("instance warning " + arg0);

        }

        public void error(SAXParseException arg0) throws SAXException
        {
                System.out.println("instance error " + arg0);
                Node node = null;

                node =
                        (Node) validator.getProperty( 
"http://apache.org/xml/properties/dom/current-element-node";);

                if (node != null)
                {
                        System.out.println("node = " + node.getNodeName());
                }
                else
                        System.out.println("Node = null");
                //e.printStackTrace();


        }

        public void fatalError(SAXParseException arg0) throws SAXException
        {
                System.out.println("instance fatal " + arg0);

        }
        //      DefaultHandler contain no-op implementations for all SAX events.
        // This class should override methods to capture the events of interest.

        public static void main(String[] args)
        {
                System.out.println("Progrma  started");

                File xmlFile = new File(args[0]);
                if (!xmlFile.exists())
                {
                        System.out.println("cannot find xml file " + args[0]);
                        System.exit(0);
                }

                TestCurrentNode testSax = new TestCurrentNode();
                testSax.parseDom(xmlFile, args[1]);
                System.out.println("Program ended");

        }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to