manual-php
Version:
509 lines (395 loc) • 14 kB
Markdown
# SimpleXML
* [SimpleXMLElement](#simplexmlelement)
* [SimpleXMLElement::__construct - 创建一个新的 SimpleXMLElement 对象](#simplexmlelementconstruct)
* [SimpleXMLElement::addAttribute - 给 SimpleXMLElement 对象添加属性](#simplexmlelementaddattribute)
* [SimpleXMLElement::addChild - 将子元素添加到 XML 节点](#simplexmlelementaddchild)
* [SimpleXMLElement::asXML - 返回基于 SimpleXMLElement 对象的格式正确的 XML 字符串](#simplexmlelementasxml)
* [SimpleXMLElement::attributes - 标识元素的属性](#simplexmlelementattributes)
* [SimpleXMLElement::children - 查找指定节点的子代](#simplexmlelementchildren)
* [SimpleXMLElement::count - 统计元素的子代数量](#simplexmlelementcount)
* [SimpleXMLElement::getDocNamespaces - 返回文档中声明的名称空间](#simplexmlelementgetdocnamespaces)
* [SimpleXMLElement::getName - 获取 XML 元素的名称](#simplexmlelementgetname)
* [SimpleXMLElement::getNamespaces - 返回文档中使用的名称空间](#simplexmlelementgetnamespaces)
* [SimpleXMLElement::registerXPathNamespace - 为下一个 XPath 查询创建一个命名空间下文](#simplexmlelementregisterxpathnamespace)
* [SimpleXMLElement::saveXML - 返回基于 SimpleXMLElement 对象的格式正确的 XML 字符串](#simplexmlelementsavexml)
* [SimpleXMLElement::__toString - 返回字符串内容](#simplexmlelementtostring)
* [SimpleXMLElement::xpath - 对 XML 数据进行 XPath 查询](#simplexmlelementxpath)
* [SimpleXMLIterator](#simplexmliterator)
* [SimpleXMLIterator::current - 返回当前元素](#simplexmliteratorcurrent)
* [SimpleXMLIterator::getChildren - 返回当前元素的子元素](#simplexmliteratorgetchildren)
* [SimpleXMLIterator::hasChildren - 检查当前元素是否具有子元素](#simplexmliteratorhaschildren)
* [SimpleXMLIterator::key - 返回当前的元素名](#simplexmliteratorkey)
* [SimpleXMLIterator::next - 移动至下一个元素](#simplexmliteratornext)
* [SimpleXMLIterator::rewind - 倒回到第一个元素](#simplexmliteratorrewind)
* [SimpleXMLIterator::valid - 检查当前元素是否有效](#simplexmliteratorvalid)
* [SimpleXML](#simplexml)
* [simplexml_import_dom - 从 DOM 节点获取 SimpleXMLElement 对象](#simplexmlimportdom)
* [simplexml_load_file - 将 XML 文件解析为对象](#simplexmlloadfile)
* [simplexml_load_string - 将 XML 字符串解析为对象](#simplexmlloadstring)
## SimpleXMLElement
### SimpleXMLElement::__construct
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>Here is some text.</body>
</document>
XML;
var_dump(new SimpleXMLElement($xml)); // object(SimpleXMLElement)#1 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }
```
### SimpleXMLElement::addAttribute
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>Here is some text.</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
$xml->addAttribute('foo', 'bar');
var_dump($xml); // object(SimpleXMLElement)#1 (3) { ["@attributes"]=> array(1) { ["foo"]=> string(3) "bar" } ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }
```
### SimpleXMLElement::addChild
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
</document>
XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->addChild('body', 'Here is some text.')); // object(SimpleXMLElement)#2 (1) { [0]=> string(18) "Here is some text." }
var_dump($xml); // object(SimpleXMLElement)#1 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }
```
### SimpleXMLElement::asXML
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>Here is some text.</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
echo $xml->asXML();
```
### SimpleXMLElement::attributes
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document foo="bar">
<title>XML</title>
<body>Here is some text.</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->attributes()); // object(SimpleXMLElement)#2 (1) { ["@attributes"]=> array(1) { ["foo"]=> string(3) "bar" } }
```
### SimpleXMLElement::children
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->children()); // object(SimpleXMLElement)#2 (2) { ["title"]=> string(3) "XML" ["body"]=> object(SimpleXMLElement)#4 (1) { ["p"]=> string(18) "Here is some text." } }
var_dump($xml->children()->body->children()); // object(SimpleXMLElement)#4 (1) { ["p"]=> string(18) "Here is some text." }
```
### SimpleXMLElement::count
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->count()); // int(2)
```
### SimpleXMLElement::getDocNamespaces
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:title="http://www.example.com/title" xmlns:body="http://www.example.com/body" xmlns:p="http://www.example.com/p">
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->getDocNamespaces()); // array(3) { ["title"]=> string(28) "http://www.example.com/title" ["body"]=> string(27) "http://www.example.com/body" ["p"]=> string(24) "http://www.example.com/p" }
```
### SimpleXMLElement::getName
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
foreach ($xml->children() as $children) {
var_dump($children->getName());
}
// string(5) "title"
// string(4) "body"
```
### SimpleXMLElement::getNamespaces
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:title="http://www.example.com/title" xmlns:body="http://www.example.com/body" xmlns:p="http://www.example.com/p">
<title:title>XML</title:title>
<body:body>
<p>Here is some text.</p>
</body:body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->getNamespaces(true)); // array(2) { ["title"]=> string(28) "http://www.example.com/title" ["body"]=> string(27) "http://www.example.com/body" }
```
### SimpleXMLElement::registerXPathNamespace
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:title="http://www.example.com/title">
<title:title>XML</title:title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
$xml->registerXPathNamespace('title', 'http://www.example.com/title');
var_dump($xml->xpath('//title:title')); // array(1) { [0]=> object(SimpleXMLElement)#2 (1) { [0]=> string(3) "XML" } }
```
### SimpleXMLElement::saveXML
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>Here is some text.</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
echo $xml->saveXML();
```
### SimpleXMLElement::__toString
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
echo $xml;
print $xml;
```
### SimpleXMLElement::xpath
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xml = new SimpleXMLElement($xml);
var_dump($xml->xpath('/document/body')); // array(1) { [0]=> object(SimpleXMLElement)#2 (1) { ["p"]=> string(18) "Here is some text." } }
```
## SimpleXMLIterator
### SimpleXMLIterator::current
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xmlIterator = new SimpleXMLIterator($xml);
var_dump($xmlIterator->current()); // NULL
$xmlIterator->rewind();
var_dump($xmlIterator->current()); // object(SimpleXMLIterator)#2 (1) { [0]=> string(3) "XML" }
```
### SimpleXMLIterator::getChildren
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
var_dump($xmlIterator->getChildren()); // object(SimpleXMLIterator)#2 (1) { [0]=> string(3) "XML" }
$xmlIterator->next();
var_dump($xmlIterator->getChildren()); // object(SimpleXMLIterator)#2 (1) { ["p"]=> string(18) "Here is some text." }
$xmlIterator->next();
var_dump($xmlIterator->getChildren()); // NULL
```
### SimpleXMLIterator::hasChildren
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
var_dump($xmlIterator->hasChildren()); // bool(false)
$xmlIterator->next();
var_dump($xmlIterator->hasChildren()); // bool(true)
```
### SimpleXMLIterator::key
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xmlIterator = new SimpleXMLIterator($xml);
var_dump($xmlIterator->key()); // bool(false)
$xmlIterator->rewind();
var_dump($xmlIterator->key()); // string(5) "title"
$xmlIterator->next();
var_dump($xmlIterator->key()); // string(4) "body"
```
### SimpleXMLIterator::next
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
$xmlIterator->next();
var_dump($xmlIterator->current()); // object(SimpleXMLIterator)#2 (1) { ["p"]=> string(18) "Here is some text." }
```
### SimpleXMLIterator::rewind
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
var_dump($xmlIterator->current()); // object(SimpleXMLIterator)#2 (1) { [0]=> string(3) "XML" }
```
### SimpleXMLIterator::valid
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>
<p>Here is some text.</p>
</body>
</document>
XML;
$xmlIterator = new SimpleXMLIterator($xml);
$xmlIterator->rewind();
var_dump($xmlIterator->valid()); // bool(true)
$xmlIterator->next();
var_dump($xmlIterator->valid()); // bool(true)
$xmlIterator->next();
var_dump($xmlIterator->valid()); // bool(false)
```
## SimpleXML
### simplexml_import_dom
```php
$dom = new DomDocument();
$dom->loadXML('<document><title>XML</title><body>Here is some text.</body></document>');
var_dump(simplexml_import_dom($dom)); // object(SimpleXMLElement)#2 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }
```
### simplexml_load_file
```php
var_dump(simplexml_load_file(__DIR__ . '/example.xml')); // object(SimpleXMLElement)#1 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }
```
上例中的 `example.xml` 文件内容:
```xml
xml version="1.0" encoding="UTF-8"
<document>
<title>XML</title>
<body>Here is some text.</body>
</document>
```
### simplexml_load_string
```php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XML</title>
<body>Here is some text.</body>
</document>
XML;
var_dump(simplexml_load_string($xml)); // object(SimpleXMLElement)#1 (2) { ["title"]=> string(3) "XML" ["body"]=> string(18) "Here is some text." }
```