Ref/libxml-Phpdoc专题

libxml_clear_errors

Clear libxml error buffer

说明

void <span class="methodname">libxml_clear_errors ( <span class="methodparam">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 <span class="methodname">libxml_disable_entity_loader ([ <span class="methodparam">bool $disable<span class="initializer"> = true ] )

Disable/enable the ability to load external entities. Note that disabling the loading of external entities may cause general issues with loading XML documents. However, as of libxml 2.9.0 entity substitution is disabled by default, so there is no need to disable the loading of 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 <span class="methodname">libxml_get_errors ( <span class="methodparam">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<span class="type">false <span class="methodname">libxml_get_last_error ( <span class="methodparam">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 <span class="methodname">libxml_set_external_entity_loader ( <span class="methodparam"><span class="type">callablenull $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 <span class="function">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 <span class="methodname">libxml_set_streams_context ( <span class="methodparam">resource $context )

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

参数

context
The stream context resource (created with <span class="function">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 <span class="methodname">libxml_use_internal_errors ([ <span class="methodparam"><span class="type">boolnull $use_errors = null ] )

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.

更新日志

版本 说明
8.0.0 use_errors is nullable now. Previously, its default was false.

范例

示例 #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

目录


本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除