xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeffrey S. Pedigo" <jped...@uswest.net>
Subject Re: A little help?
Date Wed, 16 Feb 2000 21:34:47 GMT
I ran into the same thing and defined an internal error handler class to
fix it:

This was clipped and modified from one of the samples (i forget which).

    class Errors implements ErrorHandler {

        Hashtable errorNodes = new Hashtable();

        public void warning(SAXParseException ex) {
            store(ex, "[Warning]");
        }

        public void error(SAXParseException ex) {
            store(ex, "[Error]");
        }

        public void fatalError(SAXParseException ex) throws SAXException
{
            store(ex, "[Fatal Error]");
        }

        public Hashtable getErrorNodes() {
            return errorNodes;
        }

        public Object getError(Node node) {
            return errorNodes.get(node);
        }

        public void clearErrors() {
            errorNodes.clear();
        }

        void store(SAXParseException ex, String type) {
            System.err.println(type + " at line number, " + 
                               ex.getLineNumber() + ": " + 
                               ex.getMessage() + "\n");
        }
    }


I then added the line:

     parser.setErrorHandler(new Errors());

before parsing the document. It now reports all errors;

Cheers,

Jeff


> 
> I'm attempting to validate the following document with the following dtd
> using the xerces RevaliatingDOMParser.
> I believe that the document is invalid but well formed. The parser corrects
> for well formedness however ignores invalid elements.
> If someone could give me a hint as  to what I am doing wrong I'd appreciate
> it.
> 
> Thanks,
>  Mike
> 
> DTD
> <?xml version="1.0" encoding="UTF-8" ?>
> <!ELEMENT Base_Element (child_One)?>
> <!ELEMENT child_One (child_two)>
> <!ELEMENT child_two (#PCDATA)>
> 
> XML Doc.
> <?xml version="1.0"?>
> <!DOCTYPE Base_Element SYSTEM "C:\ecdbtest\test.dtd">
> <Base_Element>
>   <child_One>
>       <child_two>legal</child_two>
>       <child_two>illegal</child_two>
>   </child_One>
> </Base_Element>
> 
> Parser Code.
> public ValDomParse(String xmlFileName) {
> 
>       InputSource is = createInputSource( xmlFileName );
>       depth = 0;
>       if( is != null ) {
>            RevalidatingDOMParser sp = new RevalidatingDOMParser();
>            try {
>                                                 sp.setFeature("http://xml.org/sax/features/validation",true);
>                                                 if( sp.getFeature("http://xml.org/sax/features/validation")
== false )
>                                                        System.out.println("Error Setting
validation feature");


>>>>>>>>          sp.setErrorHandler(new Errors());

>                 System.out.println("Parsing text");
>                 sp.parse( is );
>                 DocumentImpl doc = (DocumentImpl)sp.getDocument();
>                 NodeImpl node = (NodeImpl)doc.getDocumentElement();
>                 NodeImpl validNode = (NodeImpl)sp.validate( node );
>                 if( validNode != null ) {
>                     System.out.println(" Error at node " + validNode.getNodeName()
>                                             + " " + validNode.getNodeValue() );
> 
>                 } else {
>                     System.out.println("Document is Valid");
>                 }
>                 System.out.println("Completed Parsing Text");
>            }
>                                 catch( SaxException se )
>                                  .
>                                  .
> 
> Mike Andrasak
> IBM Global Services , Dept T39N-7
> Tie: 532-8688
> external: (914) 892-8688
> IBM internal email: andrasak@ibmusm68
> internet: andrasak@US.IBM.COM

-- 
Jeffrey S. Pedigo
jpedigo@uswest.net

Mime
View raw message