You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by caox <ca...@lightpole.net> on 2009/07/15 09:58:46 UTC

how to validate the input source?

Hi
    I am using the xercesc3.0.1 for xml parsing.
    How can I check whether the input source from a byte steam is 
well-formed xml format? Since I want my program to raise an exception 
when it receives files other than xml.
    Appreciate your help.
   

Re: how to validate the input source?

Posted by caox <ca...@lightpole.net>.
David Bertoni 写道:
> caox wrote:
>> David Bertoni 写道:
>>> caox wrote:
>>>> Alberto Massari 写道:
>>>>> Have a look at the MemParse sample.
>>>>>
>>>>> Alberto
>>>>>
>>>>> caox wrote:
>>>>>> Hi
>>>>>>    I am using the xercesc3.0.1 for xml parsing.
>>>>>>    How can I check whether the input source from a byte steam is 
>>>>>> well-formed xml format? Since I want my program to raise an 
>>>>>> exception when it receives files other than xml.
>>>>>>    Appreciate your help.
>>>>>>  
>>>>>
>>>>>
>>>> Thanks a lot. I have tried the sample DOMPrint which could throw an 
>>>> exception as expect when encountered a bad-formed xml file.
>>>> But when I use xqilla to create a DOMParser, it seems can accept 
>>>> all kinds of files. The code is below:
>>>>
>>>> AutoRelease<DOMLSParser> 
>>>> parser(xqillaImplementation->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 
>>>> 0));
>>>>
>>>> And this puzzled me a lot.
>>> Did you create a custom ErrorHandler instance and install it in the 
>>> parser?
>>>
>>> Dave
>>>
>>>
>> I didn't.But how to install a ErrorHandler instance in a DOMLSParser 
>> instance. I don't find the setXXX() method for this.
> OK, you actually need a DOMErrorHandler, rather than an ErrorHandler.
>
> The DOMPrint sample application has an example of setting the correct 
> DOMConfiguration property.  To adapt it to your use, just call 
> DOMLSParser::getDomConfig() and the set the property:
>
>     DOMErrorHandler* myErrorHandler = new myDOMErrorHandler();
>     DOMConfiguration* config = parser->getDomConfig();
>     config->setParameter(XMLUni::fgDOMErrorHandler, myErrorHandler);
>
> Dave
>
>
I have modified the code in DOMPrint sample to use DOMLSParserImpl 
instead of XercesDOMParser for testing, like below:

    DOMLSParserImpl* parser = new DOMLSParserImpl;
    DOMConfiguration* domConfig = parser->getDomConfig();

    DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
    domConfig->setParameter(XMLUni::fgDOMErrorHandler, errReporter);

And the result is quite different from using XercesDOMParser.

    XercesDOMParser *parser = new XercesDOMParser;

    DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();
    parser->setErrorHandler(errReporter);

The code has been attached. Could you please find where the problem is?

Re: how to validate the input source?

Posted by David Bertoni <db...@apache.org>.
caox wrote:
> David Bertoni 写道:
>> caox wrote:
>>> Alberto Massari 写道:
>>>> Have a look at the MemParse sample.
>>>>
>>>> Alberto
>>>>
>>>> caox wrote:
>>>>> Hi
>>>>>    I am using the xercesc3.0.1 for xml parsing.
>>>>>    How can I check whether the input source from a byte steam is 
>>>>> well-formed xml format? Since I want my program to raise an 
>>>>> exception when it receives files other than xml.
>>>>>    Appreciate your help.
>>>>>  
>>>>
>>>>
>>> Thanks a lot. I have tried the sample DOMPrint which could throw an 
>>> exception as expect when encountered a bad-formed xml file.
>>> But when I use xqilla to create a DOMParser, it seems can accept all 
>>> kinds of files. The code is below:
>>>
>>> AutoRelease<DOMLSParser> 
>>> parser(xqillaImplementation->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 
>>> 0));
>>>
>>> And this puzzled me a lot.
>> Did you create a custom ErrorHandler instance and install it in the 
>> parser?
>>
>> Dave
>>
>>
> I didn't.But how to install a ErrorHandler instance in a DOMLSParser 
> instance. I don't find the setXXX() method for this.
OK, you actually need a DOMErrorHandler, rather than an ErrorHandler.

The DOMPrint sample application has an example of setting the correct 
DOMConfiguration property.  To adapt it to your use, just call 
DOMLSParser::getDomConfig() and the set the property:

     DOMErrorHandler* myErrorHandler = new myDOMErrorHandler();
     DOMConfiguration* config = parser->getDomConfig();
     config->setParameter(XMLUni::fgDOMErrorHandler, myErrorHandler);

Dave


Re: how to validate the input source?

Posted by caox <ca...@lightpole.net>.
David Bertoni 写道:
> caox wrote:
>> Alberto Massari 写道:
>>> Have a look at the MemParse sample.
>>>
>>> Alberto
>>>
>>> caox wrote:
>>>> Hi
>>>>    I am using the xercesc3.0.1 for xml parsing.
>>>>    How can I check whether the input source from a byte steam is 
>>>> well-formed xml format? Since I want my program to raise an 
>>>> exception when it receives files other than xml.
>>>>    Appreciate your help.
>>>>  
>>>
>>>
>> Thanks a lot. I have tried the sample DOMPrint which could throw an 
>> exception as expect when encountered a bad-formed xml file.
>> But when I use xqilla to create a DOMParser, it seems can accept all 
>> kinds of files. The code is below:
>>
>> AutoRelease<DOMLSParser> 
>> parser(xqillaImplementation->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 
>> 0));
>>
>> And this puzzled me a lot.
> Did you create a custom ErrorHandler instance and install it in the 
> parser?
>
> Dave
>
>
I didn't.But how to install a ErrorHandler instance in a DOMLSParser 
instance. I don't find the setXXX() method for this.

Re: how to validate the input source?

Posted by David Bertoni <db...@apache.org>.
caox wrote:
> Alberto Massari 写道:
>> Have a look at the MemParse sample.
>>
>> Alberto
>>
>> caox wrote:
>>> Hi
>>>    I am using the xercesc3.0.1 for xml parsing.
>>>    How can I check whether the input source from a byte steam is 
>>> well-formed xml format? Since I want my program to raise an exception 
>>> when it receives files other than xml.
>>>    Appreciate your help.
>>>  
>>
>>
> Thanks a lot. I have tried the sample DOMPrint which could throw an 
> exception as expect when encountered a bad-formed xml file.
> But when I use xqilla to create a DOMParser, it seems can accept all 
> kinds of files. The code is below:
> 
> AutoRelease<DOMLSParser> 
> parser(xqillaImplementation->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 
> 0));
> 
> And this puzzled me a lot.
Did you create a custom ErrorHandler instance and install it in the parser?

Dave


Re: how to validate the input source?

Posted by caox <ca...@lightpole.net>.
Alberto Massari 写道:
> Have a look at the MemParse sample.
>
> Alberto
>
> caox wrote:
>> Hi
>>    I am using the xercesc3.0.1 for xml parsing.
>>    How can I check whether the input source from a byte steam is 
>> well-formed xml format? Since I want my program to raise an exception 
>> when it receives files other than xml.
>>    Appreciate your help.
>>  
>
>
Thanks a lot. I have tried the sample DOMPrint which could throw an 
exception as expect when encountered a bad-formed xml file.
But when I use xqilla to create a DOMParser, it seems can accept all 
kinds of files. The code is below:

AutoRelease<DOMLSParser> 
parser(xqillaImplementation->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 
0));

And this puzzled me a lot.

Re: how to validate the input source?

Posted by Alberto Massari <am...@datadirect.com>.
Have a look at the MemParse sample.

Alberto

caox wrote:
> Hi
>    I am using the xercesc3.0.1 for xml parsing.
>    How can I check whether the input source from a byte steam is 
> well-formed xml format? Since I want my program to raise an exception 
> when it receives files other than xml.
>    Appreciate your help.
>