Re: [xml] XML Catalog problem



Daniel Veillard wrote:
On Mon, Feb 13, 2006 at 02:39:49PM +0000, Chris Wareham wrote:
If I deliberately break my NewsML file by adding an invalid element, I
get an error message on the console, but the xmlReadFile function still
returns a non-NULL xmlDocPtr. How should I test the validity of the XML
after a call to xmlReadFile?

  catch the error callback (validity errors are not fatal per the spec)
I pointed less than 2 days ago on the list how to do this.
  Or access directly the parser context, create it, use xmlCtxtReadDoc
and then check ctxt->valid

  If you don't care about the tree, don't use xmlRead... use a reader
set it up to validate and ask the reader for validity at the end of the
parse.

Daniel


I have now amended my validation functions to the following:

int
newsmlparser_set_xml(NewsMLParser *parser, const char *xml)
{
    parser->ctxt = xmlNewParserCtxt();

    if (parser->ctxt == NULL) {
        parser->status = NEWSMLPARSER_OUT_OF_MEMORY;
        return 1;
    }

parser->doc = xmlCtxtReadFile(parser->ctxt, xml, NULL, XML_PARSE_DTDVALID);

    if (parser->doc == NULL || parser->ctxt->valid == 0) {
        parser->status = NEWSMLPARSER_MALFORMED_XML;
        return 1;
    }

    return 0;
}

void
newsmlparser_clear_xml(NewsMLParser *parser)
{
    if (parser->ctxt) {
        xmlFreeParserCtxt(parser->ctxt);
        parser->ctxt = NULL;
    }

    if (parser->doc) {
        xmlFreeDoc(parser->doc);
        parser->doc = NULL;
    }
}

This does exactly what I need. My original problem with catalogs was caused by the way putenv(3) is implemented on Solaris. On my development machine, which runs NetBSD, I can use setenv(3). As Solaris 8 lacks a setenv function, I wrote my own version that calls putenv. On NetBSD (and Linux I believe) putenv calls strdup() on the passed string. On Solaris it doesn't copy the string, so my local buffer was being referenced after the function returned ... Yuck.

Chris




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]