php - 扩展 DOMElement 对象

标签 php xml dom

如何扩展文档对象模型提供的对象?好像没办法according to this issue .

class Application_Model_XmlSchema extends DOMElement
{
    const ELEMENT_NAME = 'schema';

    /**
     * @var DOMElement
     */
    private $_schema;

    /**
     * @param DOMDocument $document
     * @return void
     */
    public function __construct(DOMDocument $document) {
        $this->setSchema($document->getElementsByTagName(self::ELEMENT_NAME)->item(0));
    }

    /**
     * @param DOMElement $schema
     * @return void
     */
    public function setSchema(DOMElement $schema){
        $this->_schema = $schema;
    }

    /**
     * @return DOMElement
     */
    public function getSchema(){
        return $this->_schema;
    }

    /**
     * @param  string $name
     * @param  array $arguments
     * @return mixed
     */
    public function __call($name, $arguments) {
        if (method_exists($this->_schema, $name)) {
            return call_user_func_array(
                array($this->_schema, $name),
                $arguments
            );
        }
    }
}

$version = $this->getRequest()->getParam('version', null);
$encoding = $this->getRequest()->getParam('encoding', null);
$source = 'http://www.w3.org/2001/XMLSchema.xsd';

$document = new DOMDocument($version, $encoding);
$document->load($source);

$xmlSchema = new Application_Model_XmlSchema($document);
$xmlSchema->getAttribute('version');

我收到一个错误:

Warning: DOMElement::getAttribute(): Couldn't fetch Application_Model_XmlSchema in C:\Nevermind.php on line newvermind

最佳答案

试试这个:http://www.php.net/manual/en/domdocument.registernodeclass.php

我在 DOMDocument 扩展类中使用它,它工作得很好,允许我向 DOMNode 和 DOMElement 添加方法。

关于php - 扩展 DOMElement 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2398429/

相关文章:

php - 正确设置curl php cookie

javascript - 如何使用 javascript/Jquery 在变量中使用 .text() 方法

javascript - 在开发模式下看到开发者控制台中的 Angular 6 单击绑定(bind)吗?

php - 使用 PHP 和 MySQL 进行搜索过滤

php - 刷新页面并更改 1 个 $_GET 变量

c# - 在 C# 中,如何将 POCO 序列化为带有大量花哨标记的 XML?

c# - 序列化属性的内容,但不序列化属性名称?

android - 将 XML 字符串编码为 EXI 并通过 websocket 发送

javascript - 重置游戏逻辑

php - body 图像定位与 body