xml validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anish249
    New Member
    • Sep 2010
    • 3

    xml validation

    hi all,

    i developed xml business component,where iam doing valdation based on schema.it is working fine when i pass xml and the schema used in xml.

    if xml doc doesnt follow schema i am throwing exception
    or else returning true.

    but my problem is when i pass xml and other schema that is not defined in xml.At that time it is still validating
    and throwing exception.

    instead of getting exception schema not found.

    please any one give inptust to resolve this issue.

    here is my code
    Code:
    	public boolean performValidation(String pFileXmlOne, String... pSchemaNames)
    			throws CAFException {
    
    
     		SchemaFactory factory = SchemaFactory
    				.newInstance("http://www.w3.org/2001/XMLSchema");
    
    		Source[] lStreamSources = null;
    
    		Schema schema = null;
    
    		Validator lValidator = null;
    
    		Source lSource = new StreamSource(pFileXmlOne);
    
    		// split schema string by delimiter
    
    		lStreamSources = getStreamSource(pSchemaNames);
    		logger.log(Level.INFO, myClassName + "total no of schemas"
    				+ lStreamSources.length);
    		try {
    			schema = factory.newSchema(lStreamSources);
    		} catch (SAXException e) {
    			// TODO Auto-generated catch block
    			logger.log(Level.INFO, myClassName
    					+ "sax exception in creating schema" + e.getMessage());
    			throw new SAXException(e.getMessage());
    		}
    		logger.log(Level.INFO, myClassName
    				+ " before creating validator object");
    		lValidator = schema.newValidator();
    		logger
    				.log(Level.INFO, myClassName
    						+ " after creating validator object");
    		try {
    			logger.log(Level.INFO, myClassName
    					+ " before performing validation");
    			lValidator.validate(lSource);
    			lValidator.setErrorHandler(new SimpleErrorHandler());
    			 
    			logger
    					.log(Level.INFO, myClassName
    							+ " after performing validation");
    		} 
    		catch (SAXException e) {
    			// TODO Auto-generated catch block
    			logger.log(Level.INFO, myClassName + "  sax exception"
    					+ e.getMessage());
    			throw new SAXException(e.getMessage());
    		} catch (IOException e) {
    			logger.log(Level.INFO, myClassName + " IOException"
    					+ e.getMessage());
    			throw new CAFException(e.getMessage());
    		}
    		catch(Exception e)
    		{
    			throw new SAXException("xml do not contain xsd,dtd|| xml is not following dtd or xsd");
    		}
    
    		return true;
    
    	}
    please help me to resolve this issue
    Last edited by jkmyoung; Sep 14 '10, 01:39 PM. Reason: code tags
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Sorry? Is your problem:
    1. Your xml contains elements from another schema
    2. Your schema is not passed in at all?

    Suggestions:
    Catch this particular exception and pass appropriate message or throw appropriate error.
    Perhaps have an any tag in your schema to allow elements from different namespaces.

    Comment

    • anish249
      New Member
      • Sep 2010
      • 3

      #3
      xml validation

      hi

      when i pass xml and other schema it is still validating.iam using jdk1.5 feature to validate and iam throwing exception.

      but problem is iam not getting exception as schema not found exceptionon.whi ch is my correct msg.

      please can any one help to resolve this issue.

      thanks in advance.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        What exception are you getting? Which catch block is the exception falling in?

        Comment

        • anish249
          New Member
          • Sep 2010
          • 3

          #5
          xml validation

          for input of xml,schema it is alway going to

          catch (SAXException e) {
          throw new SAXException(e. getMessage());
          }

          for input of xml,dtd
          catch (IOException e) {
          throw new IOException(e.g etMessage());
          }

          thanks in advance.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            You need to have some way of filtering those messages, probably using the Message of the exception. Check if the string contains a specified error, and throw a different exception if this is the case.

            Comment

            Working...