You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Suk Tae Kyung <tk...@penta.co.kr> on 2000/08/30 02:49:58 UTC

Q:XML Validation check problem

Hi. I'm using Xerces 1.1.3 and trying validation check.

But it seems that Xerces does not do validation check.

I dought if i made a mistake. So i expect some help.

Here is my sample. 

<!-- This is  test.xml -->
<?xml version="1.0" ?>
<!DOCTYPE TOP [
 <!ELEMENT TOP (SECOND,THIRD,FOURTH)>
 <!ELEMENT SECOND (#PCDATA)>
 <!ELEMENT THIRD (#PCDATA)>
 <!ELEMENT FOURTH (#PCDATA)>
]>

<TOP>
 <SECOND1>
   This is second!
 </SECOND1>
 <THIRD>
   This is third!
 </THIRD>
</TOP>


This is well-formed document. But not valid document. Because DTD and XML entity does not match.

To check validation, i wrote test program like this.

////////////////////////////////////////////////////////////////////////////////////////
    import java.io.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    import org.apache.xerces.parsers.*;

    public class TestValidation {
 
     public static void  main(String [] args) {
      String filename = "d:\\temp\\Hyundai\\test.xml";
      File file = new File(filename);
      FileReader fReader = null;
      try {
       fReader = new FileReader(file);
      }
      catch (Exception e) {
       System.out.println("File not found");
      }
  
      InputSource is = new InputSource(fReader);
      DOMParser  parser = new DOMParser();  // I tested SAXParser. But result was same
  
      // Setting validation check True 
      try {
         parser.setFeature("http://xml.org/sax/features/validation", true);    // ===> [A]
   } catch (SAXException e) {
         System.out.println("error in setting up parser feature");
      }
  
      // Parsing sample xml 
      try {
       parser.parse(is);     // ===> [B]
     }
      catch(SAXException se) {   // ===> [C]
       System.out.println("Sax Exception");
       System.out.println(se.getMessage());  // Get message of well-formed or validation
      }
      catch(IOException ioe) {
       System.out.println("IO Exception");
      }
  
     }  // End of main
 
    } // End of class
////////////////////////////////////////////////////////////////////////////////////////

At [A] i set validation checking to "true.
At [B] parsed test document.
At [C] expect to catch well-formed error and validation error.

But this program does not catch any SAXException. At first, i dought if this program

isn't correct. So i changed test.xml to non well-formed document. This time, well-formed error was

caught. Thus, i dought if Xerces doesn't do validation check. 

Any comment would be helpful. Thanks in advance.

Re: Q:XML Validation check problem

Posted by Suk Tae Kyung <tk...@penta.co.kr>.
Thank you for answer. Here is correct source. This is well functional
well-formed checker and validation checker. I hope that this could
be reference.

///////////////////////////////////////////////////////////////////
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.apache.xerces.parsers.*;
import org.xml.sax.helpers.*;

public class TestValidation {
 
 public static void  main(String [] args) {
  String filename = "d:\\temp\\Hyundai\\test.xml";
  File file = new File(filename);
  FileReader fReader = null;
  try {
   fReader = new FileReader(file);
  }
  catch (Exception e) {
   System.out.println("File not found");
  }
  
  InputSource is = new InputSource(fReader);
  DOMParser  parser = new DOMParser();
  
  TErrorHandler handler = new TErrorHandler();
  parser.setErrorHandler(handler);
  
  // Setting validation check True 
  try {
     parser.setFeature("http://xml.org/sax/features/validation", true);
  } catch (SAXException e) {
     System.out.println("error in setting up parser feature");
  }
  
  // Parsing sample xml 
  try {
   parser.parse(is);
  }
  catch(SAXException se) {
   System.out.println("Sax Exception");
   System.out.println(se.getMessage());  // Get message of well-formed or validation
  }
  catch(IOException ioe) {
   System.out.println("IO Exception");
  }
  
 }  // End of main
 
} // End of class
 
class TErrorHandler implements ErrorHandler {
 public void warning(SAXParseException exception) throws SAXException {
  System.out.println("Warning");
  System.out.println(exception.getMessage());
 }
 
 public void error(SAXParseException exception) throws SAXException {
  System.out.println("Error");
  System.out.println(exception.getMessage());
 }

    public void fatalError(SAXParseException exception) throws SAXException {
     System.out.println("fatalError");
  System.out.println(exception.getMessage());
    }
} 
  ----- Original Message ----- 
  From: Eric Ye 
  To: xerces-j-dev@xml.apache.org 
  Sent: Wednesday, August 30, 2000 9:53 AM
  Subject: Re: Q:XML Validation check problem


  You need to implement a Errorhandler, and register it to the parser. Please take a look at examples in samples directory, eg. dom/DOMCount
  _____

  Eric Ye * IBM, JTC - Silicon Valley * ericye@locus.apache.org

    ----- Original Message ----- 
    From: Suk Tae Kyung 
    To: Xerces 
    Sent: Tuesday, August 29, 2000 5:49 PM
    Subject: Q:XML Validation check problem


    Hi. I'm using Xerces 1.1.3 and trying validation check.

    But it seems that Xerces does not do validation check.
     
    I dought if i made a mistake. So i expect some help.

    Here is my sample. 

    <!-- This is  test.xml -->
    <?xml version="1.0" ?>
    <!DOCTYPE TOP [
     <!ELEMENT TOP (SECOND,THIRD,FOURTH)>
     <!ELEMENT SECOND (#PCDATA)>
     <!ELEMENT THIRD (#PCDATA)>
     <!ELEMENT FOURTH (#PCDATA)>
    ]>

    <TOP>
     <SECOND1>
       This is second!
     </SECOND1>
     <THIRD>
       This is third!
     </THIRD>
    </TOP>


    This is well-formed document. But not valid document. Because DTD and XML entity does not match.

    To check validation, i wrote test program like this.

    ////////////////////////////////////////////////////////////////////////////////////////
        import java.io.*;
        import org.w3c.dom.*;
        import org.xml.sax.*;
        import org.apache.xerces.parsers.*;

        public class TestValidation {
     
         public static void  main(String [] args) {
          String filename = "d:\\temp\\Hyundai\\test.xml";
          File file = new File(filename);
          FileReader fReader = null;
          try {
           fReader = new FileReader(file);
          }
          catch (Exception e) {
           System.out.println("File not found");
          }
      
          InputSource is = new InputSource(fReader);
          DOMParser  parser = new DOMParser();  // I tested SAXParser. But result was same
      
          // Setting validation check True 
          try {
             parser.setFeature("http://xml.org/sax/features/validation", true);    // ===> [A]
       } catch (SAXException e) {
             System.out.println("error in setting up parser feature");
          }
      
          // Parsing sample xml 
          try {
           parser.parse(is);     // ===> [B]
         }
          catch(SAXException se) {   // ===> [C]
           System.out.println("Sax Exception");
           System.out.println(se.getMessage());  // Get message of well-formed or validation
          }
          catch(IOException ioe) {
           System.out.println("IO Exception");
          }
      
         }  // End of main
     
        } // End of class
    ////////////////////////////////////////////////////////////////////////////////////////

    At [A] i set validation checking to "true.
    At [B] parsed test document.
    At [C] expect to catch well-formed error and validation error.

    But this program does not catch any SAXException. At first, i dought if this program

    isn't correct. So i changed test.xml to non well-formed document. This time, well-formed error was

    caught. Thus, i dought if Xerces doesn't do validation check. 

    Any comment would be helpful. Thanks in advance.

Re: Q:XML Validation check problem

Posted by Eric Ye <er...@locus.apache.org>.
You need to implement a Errorhandler, and register it to the parser. Please take a look at examples in samples directory, eg. dom/DOMCount
_____

Eric Ye * IBM, JTC - Silicon Valley * ericye@locus.apache.org

  ----- Original Message ----- 
  From: Suk Tae Kyung 
  To: Xerces 
  Sent: Tuesday, August 29, 2000 5:49 PM
  Subject: Q:XML Validation check problem


  Hi. I'm using Xerces 1.1.3 and trying validation check.

  But it seems that Xerces does not do validation check.
   
  I dought if i made a mistake. So i expect some help.

  Here is my sample. 

  <!-- This is  test.xml -->
  <?xml version="1.0" ?>
  <!DOCTYPE TOP [
   <!ELEMENT TOP (SECOND,THIRD,FOURTH)>
   <!ELEMENT SECOND (#PCDATA)>
   <!ELEMENT THIRD (#PCDATA)>
   <!ELEMENT FOURTH (#PCDATA)>
  ]>

  <TOP>
   <SECOND1>
     This is second!
   </SECOND1>
   <THIRD>
     This is third!
   </THIRD>
  </TOP>


  This is well-formed document. But not valid document. Because DTD and XML entity does not match.

  To check validation, i wrote test program like this.

  ////////////////////////////////////////////////////////////////////////////////////////
      import java.io.*;
      import org.w3c.dom.*;
      import org.xml.sax.*;
      import org.apache.xerces.parsers.*;

      public class TestValidation {
   
       public static void  main(String [] args) {
        String filename = "d:\\temp\\Hyundai\\test.xml";
        File file = new File(filename);
        FileReader fReader = null;
        try {
         fReader = new FileReader(file);
        }
        catch (Exception e) {
         System.out.println("File not found");
        }
    
        InputSource is = new InputSource(fReader);
        DOMParser  parser = new DOMParser();  // I tested SAXParser. But result was same
    
        // Setting validation check True 
        try {
           parser.setFeature("http://xml.org/sax/features/validation", true);    // ===> [A]
     } catch (SAXException e) {
           System.out.println("error in setting up parser feature");
        }
    
        // Parsing sample xml 
        try {
         parser.parse(is);     // ===> [B]
       }
        catch(SAXException se) {   // ===> [C]
         System.out.println("Sax Exception");
         System.out.println(se.getMessage());  // Get message of well-formed or validation
        }
        catch(IOException ioe) {
         System.out.println("IO Exception");
        }
    
       }  // End of main
   
      } // End of class
  ////////////////////////////////////////////////////////////////////////////////////////

  At [A] i set validation checking to "true.
  At [B] parsed test document.
  At [C] expect to catch well-formed error and validation error.

  But this program does not catch any SAXException. At first, i dought if this program

  isn't correct. So i changed test.xml to non well-formed document. This time, well-formed error was

  caught. Thus, i dought if Xerces doesn't do validation check. 

  Any comment would be helpful. Thanks in advance.