vierbergenlars / xml
Requires (Dev)
This package is not auto-updated.
Last update: 2020-03-02 04:24:40 UTC
README
SimpleXML 的一个合理的包装器
安装
composer require vierbergenlars/xml:@stable
技巧:你应该浏览
vierbergenlars/xml
页面以选择要使用的稳定版本,避免使用@stable
元数据约束。
使用
你的代码中应该只实例化 XmlElement
类。
use vierbergenlars\Xml\XmlElement; $simpleXml = new \SimpleXMLElement($xml); $xmlElement = new XmlElement($simpleXml);
以下示例基于以下 xml 结构
<?xml version="1.0" encoding="UTF-8"?> <result page="1" items_per_page="2" total="2"> <entry id="30"> <filename><![CDATA[Screenshot from 2013-10-21 19:31:42.png]]></filename> <course id="1"><![CDATA[Algebra]]></course> <type id="1"><![CDATA[Exercise]]></type> <link rel="self" href="/files/30" type="api"/> <link rel="self" href="http://example.invalid/file/Algebra/Exercise/Screenshot%20from%202013-10-21%2019:31:42.png" type="www"/> <link rel="author" href="/users/vierbergenlars"/> </entry> <entry id="40"> <filename><![CDATA[Screen Shot 2013-11-17 at 13.34.30.xcf]]></filename> <course id="1"><![CDATA[Analyse]]></course> <type id="1"><![CDATA[Slides]]></type> <link rel="self" href="/files/40" type="api"/> <link rel="self" href="http://example.invalid/file/Algebra/Slides/Screen%20Shot%202013-11-17%20at%2013.34.30.xcf" type="www"/> <link rel="author" href="/users/vierbergenlars"/> </entry> </result>
此示例是 example.php 的副本,该文件可用于实验,位于存储库中。
$totalPages = ceil($xmlElement->attr('total') / $xmlElement->attr('items_per_page')); echo $xmlElement->attr('page') . '/' . $totalPages . "\n";
XmlElement::attr($name)
获取当前元素上 XML 属性 $name
的值。
$files = $xmlElement->children(); /* @var $files XmlCollectionInterface */
XmlElement::children()
获取当前元素的直接子元素。结果实现了 XmlCollectionInterface
。它是一个 Iterator
,实现了 ArrayAccess
和 Countable
。
为了在 PHP 5.3 中实现更简单的链式调用,提供了一个 $XmlCollectionInterface->get($idx)
函数,其效果与 $XmlCollectionInterface[$idx]
相同。
提供了一个 ->find($attrs = array())
函数,该函数返回一个新的 XmlCollectionInterface
,其中只包含传递给函数的属性。
foreach($files as $file) { /* @var $file XmlElementInterface */ echo 'File ' . $file->attr('id') . ":\n"; echo ' Filename: ' . $file->child('filename')->text() . "\n";
选择第一个元素,并返回其标签之间的文本(使用 ->text()
)。`
echo " Links:\n"; foreach($file->children('link') as $link) {
在此,对 $file
的子元素进行了筛选,只包含标签名为 <link>
的元素。
/* @var $link XmlElementInterface */ echo ' ' . $link->attr('href'); foreach($link->attributes() as $attr => $value) {
选择所有来自 <link>
元素的属性。 $link->attributes()
返回一个实现了 XmlAttributesInterface
的实现。该对象作为一个只读的 array
,属性名称作为键,属性值作为值。此外,它还有一个 ->get($idx)
函数,用于更简单的链式调用。
if($attr != 'href') echo ' (' . $attr . '=' . $value . ')'; } echo "\n"; } echo ' Weblink: ' . $file->children('link')->find(array('rel' => 'self', 'type' => 'www'))->get(0)->attr('href') . "\n";
这是使用 XmlCollectionInterface::find()
仅选择具有所需属性的 <link>
元素的示例。下面是使用 XmlElementInterface::child()
的等效示例,已被注释。
// echo ' Weblink: ' . $file->child('link', array('rel' => 'self', 'type' => 'www'))->attr('href') . "\n"; } echo 'Total: ' . $files->count() . "\n";
参考
XmlElementInterface
返回 | 函数签名 | 文档 |
---|---|---|
字符串 |
getName() |
获取元素标签名 |
字符串 |
text() |
获取元素中的文本 |
XmlElementInterface |
setText(string $text) |
将元素中的文本设置为 $text 。返回自身。 |
字符串 |
attr(string $name) |
获取元素上 $name 属性的值(等同于 ->attributes()->get($name) ) |
XmlAttributesInterface |
attributes() |
获取元素上的所有属性 |
XmlElementInterface |
child(string $name = null, array $filter = array(), int $n = 0) |
获取指定名称的 $n 个子元素(等同于 ->children($name)->find($filter)->pos($n) ) |
XmlElementInterface |
addChild(string $name) |
向当前元素添加一个具有标签名 $name 的子元素。返回的元素可以进行修改。 |
XmlElementInterface |
addChild(string $name, string $text) |
添加一个具有标签名 $name 和文本内容 $text 的子节点(->addChild($name)->setText($text) ) |
XmlElementInterface |
addChild(string $name, XmlElementInterface $element) |
添加一个具有标签名 $name 的子节点,并将其内容和属性设置为与 $element 相同 |
XmlElementInterface |
addChild(XmlElementInterface $element) |
将具有与 $element 相同的标签名、内容和属性的子节点添加到当前元素 |
XmlCollectionInterface |
children(string $name = null) |
获取元素的所有子节点(如果 $name !== null ,则只获取指定名称的子节点) |
XmlCollectionInterface |
find(array $attributes = array()) |
获取与 $attributes 中属性匹配的元素的子节点 |
XmlCollectionInterface
实现接口 Iterator
、ArrayAccess
、Countable
返回 | 函数签名 | 文档 |
---|---|---|
XmlElementInterface |
get(int $pos) |
获取给定位置的元素 |
XmlElementInterface |
add($name, $value = null) |
在父元素上调用 XmlElementInterface::addChild() |
XmlCollectionInterface |
find(array $attributes = array()) |
根据集合成员的属性过滤集合 |
int |
count() |
Countable::count() 计算集合中的元素数量 |
boolean |
offsetExists(int $offset) |
ArrayAccess::offsetExists 检查是否存在偏移量(参见 get() ) |
XmlElementInterface |
offsetGet(int $offset) |
ArrayAccess::offsetGet() 获取偏移量 |
- | offsetSet() |
抛出 LogicException 。以这种方式修改集合是不允许的 |
- | offsetUnset(int $offset) |
ArrayAcess:offsetUnset() 从集合中移除位于 $offset 的元素 |
XmlElementInterface |
current() |
Iterator::current() 返回当前元素 |
int |
key() |
Iterator::key() 返回当前元素的键 |
void |
next() |
Iterator::next() 将迭代器向前移动到下一个元素 |
void |
rewind() |
Iterator::rewind() 将迭代器重置为第一个元素 |
boolean |
valid() |
Iterator::valid() 检查当前位置是否有效 |
- 此对象实现了
ArrayAccess
$xmlCollectionInterface[$offset]
调用$xmlCollectionInterface->offsetGet($offset)
isset($xmlCollectionInterface[$offset])
调用$xmlCollectionInterface->offsetExists($offset)
$xmlCollectionInterface[$offset] = $mixed
调用$xmlCollectionInterface->offsetSet($offset, $mixed)
并抛出异常。unset($xmlCollectionInterface[$offset])
调用$xmlCollectionInterface->offsetUnset($offset)
- 此对象实现了
Countable
count($xmlCollectionInterface)
调用$xmlCollectionInterface->count()
- 此对象实现了
Iterator
foreach($xmlCollectionInterface as $key => $value){}
调用$xmlCollectionInterface->rewind()
一次,在每次迭代之前调用$xmlCollectionInterface->valid()
,如果它返回 true,则设置$key = $xmlCollectionInterface->key(); $value = $xmlCollectionInterface->current()
。最后调用$xmlCollectionInterface->next()
。
XmlAttributesInterface
实现接口 Iterator
、ArrayAccess
、Countable
返回 | 函数签名 | 文档 |
---|---|---|
string 或 null |
get(string $name) |
获取属性 $name 的值 |
XmlAttributesInterface |
set(string $name, strinv $value) |
将属性 $name 设置为值 $value 。返回自身 |
int |
count() |
Countable::count() 计算集合中的元素数量 |
boolean |
offsetExists(string $attr) |
ArrayAccess::offsetExists() 检查属性是否存在 |
string 或 null |
offsetGet(string $attr) |
ArrayAccess::offsetGet() 获取属性(参见 get() ) |
- | offsetSet(string $attr, string $val ) |
ArrayAccess:offsetSet() 将属性设置为值(参见 set() ) |
- | offsetUnset(string $attr) |
ArrayAccess::offsetUnset() 从元素中移除属性 |
字符串 |
current() |
Iterator::current() 返回当前元素 |
字符串 |
key() |
Iterator::key() 返回当前元素的键 |
void |
next() |
Iterator::next() 将迭代器向前移动到下一个元素 |
void |
rewind() |
Iterator::rewind() 将迭代器重置为第一个元素 |
boolean |
valid() |
Iterator::valid() 检查当前位置是否有效 |
-
此对象实现了
ArrayAccess
$xmlAttributesInterface[$offset]
调用$xmlAttributesInterface->offsetGet($offset)
isset($xmlAttributesInterface[$offset])
调用$xmlAttributesInterface->offsetExists($offset)
$xmlAttributesInterface[$offset] = $mixed
调用$xmlAttributesInterface->offsetSet($offset, $mixed)
unset($xmlAttributesInterface[$offset])
调用$xmlAttributesInterface->offsetUnset($offset)
-
此对象实现了
Countable
count($xmlAttributesInterface)
调用$xmlAttributesInterface->count()
-
此对象实现了
Iterator
foreach($xmlAttributesInterface as $key => $value){}
一次调用$xmlAttributesInterface->rewind()
,每次迭代前调用$xmlAttributesInterface->valid()
,如果返回 true 则设置$key = $xmlAttributesInterface->key(); $value = $xmlAttributesInterface->current()
。最后调用$xmlAttributesInterface->next()
。