一个用于管理DOM文档的库。此库是SoloProyectos PHP API的一部分。

此包的官方仓库似乎已不存在,因此已冻结此包。

安装: 723

依赖: 0

建议者: 0

安全: 0

星标: 4

关注者: 1

分支: 1

类型:php-library

1.4.1 2018-01-29 06:04 UTC

This package is not auto-updated.

Last update: 2024-05-09 20:58:59 UTC


README

一个用于以简单直观的方式管理XML文档的库。

安装

此库使用composer作为依赖项管理器。只需创建一个composer.json文件

{
    "require": {
        "php": ">=5.5.0",
        "soloproyectos-php/dom-node": "~1.0"
    }
}

然后安装依赖项

php composer.phar install

# or execute the following command if you have installed 'composer' globally
composer install 

有关更多信息,请参阅composer教程
https://getcomposer.org.cn/doc/01-basic-usage.md

方法

从给定源创建节点

  • DomNode::createFromElement($element):从DOMElement对象创建实例
  • DomNode::createFromNode($node):从DomNode对象创建实例
  • DomNode::createFromString($string):从字符串创建实例

基本方法

  • DomNode::document():获取内部DOMDocument实例
  • DomNode::elements():获取内部DOM元素
  • DomNode::name():获取节点名称
  • DomNode::parent():获取父节点或null
  • DomNode::root():获取根节点
  • DomNode::query($cssSelectors):使用CSS选择器查找节点
  • DomNode::xpath($expression):使用XPath表达式查找节点
  • DomNode::remove():从文档中删除节点
  • DomNode::clear():删除所有子节点
  • DomNode::data($name, [$value]):获取或设置任意数据
  • DomNode::append($string):追加内部XML文本
  • DomNode::prepend($string):预追加内部XML文本
  • DomNode::html([$string]):获取或设置内部XML文本
  • DomNode::text([$string]):获取或设置内部文本

属性

  • DomNode::attr($name, [$value]):获取或设置属性
  • DomNode::hasAttr($name):检查节点是否有属性

CSS属性

  • DomNode::css($name, [$value]):获取或设置CSS属性
  • DomNode::hasCss($name):检查节点是否有CSS属性

  • DomNode::addClass($className):向节点添加类
  • DomNode::hasClass($className):检查节点是否有类
  • DomNode::removeClass($className):从节点中删除类

基本示例

创建实例

创建一个简单节点

// creates a simple node with two attributes and inner text
$item = new DomNode("item", array("id" => 101, "title" => "Title 101"), "Inner text here...");
echo $item;

创建一个复杂节点

// in this case we use a callback function to add complex structures into the node
$root = new DomNode("root", function ($target) {
    // adds three subnodes
    for ($i = 0; $i < 3; $i++) {
        $target->append(new DomNode("item", array("id" => $i, "title" => "Title $i"), "This is the item $i"));
    }
    
    // appends some XML code
    $target->append("<text>This text is added to the end.</text>");
    
    // prepends some XML code
    $target->prepend("<text>This text is added to the beginning</text>");
});
echo $root;

从给定源创建实例

// creates an instance from a string
$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>');

// creates an instance from a given DOMElement
$doc = new DOMDocument("1.0", "UTF-8");
$doc->loadXML('<root><item id="101" /><item id="102" /><item id="103" /></root>');
$xml = DomNode::createFromElement($doc->documentElement);

使用query方法

您可以使用相同的query函数检索单个或多个节点。

$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>');

// selects and prints all items
$items = $xml->query("item");
foreach ($items as $item) {
    echo $item . "\n";
}

// select and prints a single item
$item = $xml->query("item[id = 102]");
echo $item;

使用attrtexthtml方法

$xml = DomNode::createFromString(file_get_contents("test.xml"));

// prints books info
$books = $xml->query("books item");
foreach ($books as $book) {
    echo "Title: " . $book->attr("title") . "\n";
    echo "Author: " . $book->attr("author_id") . "\n";
    echo "ISBN: " . $book->query("isbn")->text() . "\n";
    echo "Available: " . $book->query("available")->text() . "\n";
    echo "Description: " . $book->query("description")->text() . "\n";
    echo "---\n";
}

// gets the number of items
echo "Number of items: " . count($books);

// prints inner XML text
$genres = $xml->query("genres");
echo $genres->html();

使用attrtexthtml方法更改内容

在上一个示例中,我们使用attrtexthtml来获取内容。在本例中,我们使用相同的方法来更改文档。

$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>');

// changes or adds attributes and inner texts
$item = $xml->query("item[id = 102]");
$item->attr("id", 666);
$item->attr("title", "Item 666");
$item->text("I'm an inner text");
echo $item;

// changes inner contents
$item = $xml->query("item[id = 103]");
$item->html('<subitem>I am a subitem</subitem>');
echo $item;

使用prependappend方法

$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>');

// appends contents
$item = $xml->query("item[id = 102]");
$item->append('<subitem id="102.1" title="Subitem title">This text goes to the end...</subitem>');
echo $xml;

// appends a DomNode object
$item->append(new DomNode("subitem", array("id" => "102.1", "title" => "Subitem title"), "Some inner text here ..."));
echo $xml;

// appends a DomNode object and calls the `callback` function
$item->prepend(new DomNode("subitem", array("id" => "102.2", "title" => "Subitem title"), function ($target) {
    $target->text("I'm the first child node ...");
}));
echo $xml;

使用removeclear方法

$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>');

// removes a single item
$item = $xml->query("item[id = 103]");
$item->remove();
echo $xml;

// removes a list of items
$items = $xml->query("item:even");
$items->remove();
echo $xml;

// removes all chid nodes
$xml->clear();
echo $xml;

链式调用

您可以在同一行中连接多个方法

$xml = DomNode::createFromString('<root><item id="101" /><item id="102" /><item id="103" /></root>');

// changes and prints the node in the same line
echo $xml->query("item[id = 102]")->attr("title", "Item 102")->text("Some text...")->append("<subitem />");