libxml_clear_errors

Clear libxml error buffer

说明

void libxml_clear_errors ( void )

libxml_clear_errors clears the libxml error buffer.

返回值

没有返回值。

参见

  • libxml_get_errors
  • libxml_get_last_error

libxml_disable_entity_loader

Disable the ability to load external entities

说明

bool libxml_disable_entity_loader ([ bool $disable = TRUE ] )

Disable/enable the ability to load external entities.

参数

disable
Disable (TRUE) or enable (FALSE) libxml extensions (such as DOM, XMLWriter and XMLReader) to load external entities.

返回值

Returns the previous value.

参见

libxml_get_errors

Retrieve array of errors

说明

array libxml_get_errors ( void )

Retrieve array of errors.

返回值

Returns an array with LibXMLError objects if there are any errors in the buffer, or an empty array otherwise.

范例

示例 #1 A libxml_get_errors example

This example demonstrates how to build a simple libxml error handler.

<?php

libxml_use_internal_errors(true);

$xmlstr = <<< XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <titles>PHP: Behind the Parser</title>
 </movie>
</movies>
XML;

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

if ($doc === false) {
    $errors = libxml_get_errors();

    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";
}

?>

以上例程会输出:

  <titles>PHP: Behind the Parser</title>
----------------------------------------------^
Fatal Error 76: Opening and ending tag mismatch: titles line 4 and title
  Line: 4
  Column: 46

--------------------------------------------

参见

  • libxml_get_last_error
  • libxml_clear_errors

libxml_get_last_error

Retrieve last error from libxml

说明

LibXMLError libxml_get_last_error ( void )

Retrieve last error from libxml.

返回值

Returns a LibXMLError object if there is any error in the buffer, FALSE otherwise.

参见

  • libxml_get_errors
  • libxml_clear_errors

libxml_set_external_entity_loader

Changes the default external entity loader

说明

bool libxml_set_external_entity_loader ( callable $resolver_function )

Changes the default external entity loader.

参数

resolver_function
A callable that takes three arguments. Two strings, a public id and system id, and a context (an array with four keys) as the third argument. This callback should return a resource, a string from which a resource can be opened, or NULL.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 libxml_set_external_entity_loader example

<?php
$xml = <<<XML
<!DOCTYPE foo PUBLIC "-//FOO/BAR" "http://example.com/foobar">
<foo>bar</foo>
XML;

$dtd = <<<DTD
<!ELEMENT foo (#PCDATA)>
DTD;

libxml_set_external_entity_loader(
    function ($public, $system, $context) use($dtd) {
        var_dump($public);
        var_dump($system);
        var_dump($context);
        $f = fopen("php://temp", "r+");
        fwrite($f, $dtd);
        rewind($f);
        return $f;
    }
);

$dd = new DOMDocument;
$r  = $dd->loadXML($xml);

var_dump($dd->validate());
?>

以上例程会输出:

string(10) "-//FOO/BAR"
string(25) "http://example.com/foobar"
array(4) {
    ["directory"]    => NULL
    ["intSubName"]   => NULL
    ["extSubURI"]    => NULL
    ["extSubSystem"] => NULL
}
bool(true)

参见

  • libxml_disable_entity_loader

libxml_set_streams_context

Set the streams context for the next libxml document load or write

说明

void libxml_set_streams_context ( resource $streams_context )

Sets the streams context for the next libxml document load or write.

参数

streams_context
The stream context resource (created with stream_context_create)

返回值

没有返回值。

范例

示例 #1 A libxml_set_streams_context example

<?php

$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
    )
);

$context = stream_context_create($opts);
libxml_set_streams_context($context);

// request a file through HTTP
$doc = DOMDocument::load('http://www.example.com/file.xml');

?>

参见

  • stream_context_create

libxml_use_internal_errors

Disable libxml errors and allow user to fetch error information as needed

说明

bool libxml_use_internal_errors ([ bool $use_errors = FALSE ] )

libxml_use_internal_errors allows you to disable standard libxml errors and enable user error handling.

参数

use_errors
Enable (TRUE) user error handling or disable (FALSE) user error handling. Disabling will also clear any existing libxml errors.

返回值

This function returns the previous value of use_errors.

范例

示例 #1 A libxml_use_internal_errors example

This example demonstrates the basic usage of libxml errors and the value returned by this function.

<?php

// enable user error handling
var_dump(libxml_use_internal_errors(true));

// load the document
$doc = new DOMDocument;

if (!$doc->load('file.xml')) {
    foreach (libxml_get_errors() as $error) {
        // handle errors here
    }

    libxml_clear_errors();
}

?>

以上例程会输出:

bool(false)

参见

  • libxml_clear_errors
  • libxml_get_errors

目录