Jump to content

help with xml validator function


mpsn

Recommended Posts

Hi, I modified the php.net's xml validate function slightly to just print "Valid XML" or else print all the errors, but it is not working. It keeps outputting "Valid XML" even though I intentionally appended "SS" to the closing message tag.

 

Here is the script:

$dom_=<<<START
<?xml version="1.0" encoding="ISO-8859-1" ?>
<email>
<message>
	<to>
		<toFirstName type="common">Bob</toFirstName>
		<toLastName kind="swag">Smith</toLastName>
	</to>
	<from>
		<fromFirstName>Steve</fromFirstName>
		<fromLastName>Yzerman</fromLastName>
	</from>
</messageSS>
</email>
START;

//validate($dom_);

//FCN SOURCE:php.net in section on libxml_get_errors
libxml_use_internal_errors(true);

$xml = explode("\n", $dom_);

$errors = libxml_get_errors();

if(empty($errors)) 
    print "Valid XML";
else {
    foreach ($errors as $error) {
        echo display_xml_error($error, $xml);
    }
    libxml_clear_errors();
}

function display_xml_error($error, $xml)
{
    $return  = $xml[$error->line - 1] . "\n";
    $return .= str_repeat('-', $error->column) . "^\n";

    switch ($error->level) {
        case LIBXML_ERR_WARNING:
            $return .= "Warning $error->code: ";
            break;
         case LIBXML_ERR_ERROR:
            $return .= "Error $error->code: ";
            break;
        case LIBXML_ERR_FATAL:
            $return .= "Fatal Error $error->code: ";
            break;
    }

    $return .= trim($error->message) .
               "\n  Line: $error->line" .
               "\n  Column: $error->column";

    if ($error->file) {
        $return .= "\n  File: $error->file";
    }

    return "$return\n\n--------------------------------------------\n\n";
}

 

Any help is much appreciated, thanks.

Link to comment
https://forums.phpfreaks.com/topic/250260-help-with-xml-validator-function/
Share on other sites

Hi, can someone explain what:

 

$return  = $xml[$error->line - 1] . "\n";

    $return .= str_repeat('-', $error->column) . "^\n";

 

these two lines exactly do in the display_xml_error function.

 

Another related question to the first post in this thread is, why does nothing appear when I use (so I mean nothing is outputted in browser screen):

$doc=new DOMDocument();

$doc->loadXML($xmlstr);$xml = explode("\n", $xmlstr);

 

instead of:

$doc = simplexml_load_string($xmlstr);

$xml = explode("\n", $xmlstr);

 

Btw, I got it to work, just curious about these two points.

 

Any help much appreciated

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.