Learning
Learning

Reputation: 868

How to alert php warning messages?

function validatexml() {
    var xmlfile = "<?php echo $_SESSION['downxml'];?>";
    $.post('xmlvalidate.php', { xmlfile : xmlfile }, function (data) {
        if ($.trim(data) == 'Y') {
            alert('Xml file is valid against NLM 2.3 DTD');
        } else {
            alert('Xml file is not valid against NLM 2.3 DTD<br>Additional Note: ' + data);
        }
    });
}

xmlvalidate.php will return a warning message If I execute it through the browser. From the script (which is mentioned above) I will get an output in the variable data.

I need to alert warning messages also which is returned by xmlvalidate.php. How to do it?

I have gone through the function error_get_last() But it returns only the last warning message. I need to get all the warning messages. How can I do it?

Upvotes: 2

Views: 859

Answers (1)

hek2mgl
hek2mgl

Reputation: 158250

On PHP side you can use libxml_get_errors():

libxml_use_internal_errors(true);

/* do validation stuff here */

$errors = libxml_get_errors();

Dealing with XML errors in PHP explains that:

The libXMLError object, returned by libxml_get_errors(), contains several properties including the message, line and column (position) of the error.

There is also an example with loading invalid XML:

libxml_use_internal_errors(true);
$sxe = simplexml_load_string("<?xml version='1.0'><broken><xml></broken>");
if ($sxe === false) {
    echo "Failed loading XML\n";
    foreach(libxml_get_errors() as $error) {
        echo "\t", $error->message;
    }
}

Which outputs:

Failed loading XML
    Blank needed here
    parsing XML declaration: '?>' expected
    Opening and ending tag mismatch: xml line 1 and broken
    Premature end of data in tag broken line 1

Upvotes: 2

Related Questions