SimpleXML

目录

简介

Represents an element in an XML document.

类摘要

SimpleXMLElement

class SimpleXMLElement implements Traversable {

/* 方法 */

final public __construct ( string $data [, int $options = 0 [, bool $data_is_url = FALSE [, string $ns = "" [, bool $is_prefix = FALSE ]]]] )

public void addAttribute ( string $name [, string $value [, string $namespace ]] )

public SimpleXMLElement addChild ( string $name [, string $value [, string $namespace ]] )

public mixed asXML ([ string $filename ] )

public SimpleXMLElement attributes ([ string $ns = NULL [, bool $is_prefix = FALSE ]] )

public SimpleXMLElement children ([ string $ns [, bool $is_prefix = FALSE ]] )

public int count ( void )

public array getDocNamespaces ([ bool $recursive = FALSE [, bool $from_root = TRUE ]] )

public string getName ( void )

public array getNamespaces ([ bool $recursive = FALSE ] )

public bool registerXPathNamespace ( string $prefix , string $ns )

public string __toString ( void )

public array xpath ( string $path )

}

SimpleXMLElement::addAttribute

Adds an attribute to the SimpleXML element

说明

public void SimpleXMLElement::addAttribute ( string $name [, string $value [, string $namespace ]] )

Adds an attribute to the SimpleXML element.

参数

name
The name of the attribute to add.

value
The value of the attribute.

namespace
If specified, the namespace to which the attribute belongs.

返回值

没有返回值。

范例

Note:

Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.

示例 #1 Add attributes and children to a SimpleXML element

<?php

include 'example.php';
 
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');

$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');

$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');
 
echo $sxe->asXML();

?>

以上例程的输出类似于:

<?xml version="1.0" standalone="yes"?>
<movies type="documentary">
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#xD3;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
 <movie>
  <title>PHP2: More Parser Stories</title>
  <plot>This is all about the people who make it work.</plot>
  <characters>
   <character>
    <name>Mr. Parser</name>
    <actor>John Doe</actor>
   </character>
  </characters>
  <rating type="stars">5</rating>
 </movie>
</movies>

参见

  • SimpleXMLElement::addChild

SimpleXMLElement::addChild

Adds a child element to the XML node

说明

public SimpleXMLElement SimpleXMLElement::addChild ( string $name [, string $value [, string $namespace ]] )

Adds a child element to the node and returns a SimpleXMLElement of the child.

参数

name
The name of the child element to add.

value
If specified, the value of the child element.

namespace
If specified, the namespace to which the child element belongs.

返回值

The addChild method returns a SimpleXMLElement object representing the child added to the XML node.

范例

Note:

Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.

示例 #1 Add attributes and children to a SimpleXML element

<?php

include 'example.php';

$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');

$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');

$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');
 
echo $sxe->asXML();

?>

以上例程的输出类似于:

<?xml version="1.0" standalone="yes"?>
<movies type="documentary">
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#xD3;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
 <movie>
  <title>PHP2: More Parser Stories</title>
  <plot>This is all about the people who make it work.</plot>
  <characters>
   <character>
    <name>Mr. Parser</name>
    <actor>John Doe</actor>
   </character>
  </characters>
  <rating type="stars">5</rating>
 </movie>
</movies>

参见

  • SimpleXMLElement::addAttribute

SimpleXMLElement::asXML

Return a well-formed XML string based on SimpleXML element

说明

public mixed SimpleXMLElement::asXML ([ string $filename ] )

The asXML method formats the parent object's data in XML version 1.0.

参数

filename
If specified, the function writes the data to the file rather than returning it.

返回值

If the filename isn't specified, this function returns a string on success and FALSE on error. If the parameter is specified, it returns TRUE if the file was written successfully and FALSE otherwise.

范例

示例 #1 Get XML

<?php
$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = new SimpleXMLElement($string);

echo $xml->asXML();

?>

以上例程会输出:

<?xml version="1.0"?>
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>

asXML also works on Xpath results:

示例 #2 Using asXML() on SimpleXMLElement::xpath results

<?php
// Continued from example XML above.

/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

foreach ($result as $node) {
    echo $node->asXML();
}
?>

以上例程会输出:

<c>text</c><c>stuff</c>

参见

  • SimpleXMLElement::__toString

SimpleXMLElement::attributes

Identifies an element's attributes

说明

public SimpleXMLElement SimpleXMLElement::attributes ([ string $ns = NULL [, bool $is_prefix = FALSE ]] )

This function provides the attributes and values defined within an xml tag.

Note: SimpleXML 建起了一个给大多数方法添加迭代属性的规则。不能通过使用 class="function">var_dump 或任何可检查对象的其它东西来查看。

参数

ns
An optional namespace for the retrieved attributes

is_prefix
Default to FALSE

返回值

Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.

Returns NULL if called on a SimpleXMLElement object that already represents an attribute and not a tag.

范例

示例 #1 Interpret an XML string

<?php
$string = <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}
?>

以上例程会输出:

name="one"
game="lonely"

参见

SimpleXMLElement::children

Finds children of given node

说明

public SimpleXMLElement SimpleXMLElement::children ([ string $ns [, bool $is_prefix = FALSE ]] )

This method finds the children of an element. The result follows normal iteration rules.

Note: SimpleXML 建起了一个给大多数方法添加迭代属性的规则。不能通过使用 class="function">var_dump 或任何可检查对象的其它东西来查看。

参数

ns
An XML namespace.

is_prefix
If is_prefix is TRUE, ns will be regarded as a prefix. If FALSE, ns will be regarded as a namespace URL.

返回值

Returns a SimpleXMLElement element, whether the node has children or not.

更新日志

版本说明
5.2.0The optional parameter is_prefix was added.

范例

示例 #1 Traversing a children() pseudo-array

<?php
$xml = new SimpleXMLElement(
'<person>
 <child role="son">
  <child role="daughter"/>
 </child>
 <child role="daughter">
  <child role="son">
   <child role="son"/>
  </child>
 </child>
</person>');

foreach ($xml->children() as $second_gen) {
    echo ' The person begot a ' . $second_gen['role'];

    foreach ($second_gen->children() as $third_gen) {
        echo ' who begot a ' . $third_gen['role'] . ';';

        foreach ($third_gen->children() as $fourth_gen) {
            echo ' and that ' . $third_gen['role'] .
                ' begot a ' . $fourth_gen['role'];
        }
    }
}
?>

以上例程会输出:

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son

示例 #2 Using namespaces

<?php
$xml = '<example xmlns:foo="my.foo.urn">
  <foo:a>Apple</foo:a>
  <foo:b>Banana</foo:b>
  <c>Cherry</c>
</example>';

$sxe = new SimpleXMLElement($xml);

$kids = $sxe->children('foo');
var_dump(count($kids));

$kids = $sxe->children('foo', TRUE);
var_dump(count($kids));

$kids = $sxe->children('my.foo.urn');
var_dump(count($kids));

$kids = $sxe->children('my.foo.urn', TRUE);
var_dump(count($kids));

$kids = $sxe->children();
var_dump(count($kids));
?>
int(0)
int(2)
int(2)
int(0)
int(1)

注释

SimpleXMLElement::children returns a node object no matter if the current node has children or not. Use count on the return value to see if there are any children. As of PHP 5.3.0, SimpleXMLElement::count may be used instead.

参见

  • SimpleXMLElement::count
  • count

SimpleXMLElement::__construct

Creates a new SimpleXMLElement object

说明

final public SimpleXMLElement::__construct ( string $data [, int $options = 0 [, bool $data_is_url = FALSE [, string $ns = "" [, bool $is_prefix = FALSE ]]]] )

Creates a new SimpleXMLElement object.

参数

data
A well-formed XML string or the path or URL to an XML document if data_is_url is TRUE.

options
Optionally used to specify additional Libxml parameters, which affect reading of XML documents. Options which affect the output of XML documents (e.g. LIBXML_NOEMPTYTAG) are silently ignored.

Note:

It may be necessary to pass LIBXML_PARSEHUGE to be able to process deeply nested XML or very large text nodes.

data_is_url
By default, data_is_url is FALSE. Use TRUE to specify that data is a path or URL to an XML document instead of string data.

ns
Namespace prefix or URI.

is_prefix
TRUE if ns is a prefix, FALSE if it's a URI; defaults to FALSE.

返回值

Returns a SimpleXMLElement object representing data.

错误/异常

Produces an E_WARNING error message for each error found in the XML data and additionally throws an Exception if the XML data could not be parsed.

小贴士

Use libxml_use_internal_errors to suppress all XML errors, and libxml_get_errors to iterate over them afterwards.

范例

Note:

Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.

示例 #1 Create a SimpleXMLElement object

<?php

include 'example.php';

$sxe = new SimpleXMLElement($xmlstr);
echo $sxe->movie[0]->title;

?>

以上例程会输出:

PHP: Behind the Parser

示例 #2 Create a SimpleXMLElement object from a URL

<?php

$sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE);
echo $sxe->asXML();

?>

更新日志

版本说明
5.2.0Added the ns and is_prefix parameters.
5.1.2Added the options and data_is_url parameters.

参见

  • simplexml_load_string
  • simplexml_load_file
  • libxml_use_internal_errors

SimpleXMLElement::count

Counts the children of an element

说明

public int SimpleXMLElement::count ( void )

This method counts the number of children of an element.

返回值

Returns the number of elements of an element.

范例

示例 #1 Counting the number of children

<?php
$xml = <<<EOF
<people>
 <person name="Person 1">
  <child/>
  <child/>
  <child/>
 </person>
 <person name="Person 2">
  <child/>
  <child/>
  <child/>
  <child/>
  <child/>
 </person>
</people>
EOF;

$elem = new SimpleXMLElement($xml);

foreach ($elem as $person) {
    printf("%s has got %d children.\n", $person['name'], $person->count());
}
?>

以上例程会输出:

Person 1 has got 3 children.
Person 2 has got 5 children.

参见

  • SimpleXMLElement::children

SimpleXMLElement::getDocNamespaces

Returns namespaces declared in document

说明

public array SimpleXMLElement::getDocNamespaces ([ bool $recursive = FALSE [, bool $from_root = TRUE ]] )

Returns namespaces declared in document

参数

recursive
If specified, returns all namespaces declared in parent and child nodes. Otherwise, returns only namespaces declared in root node.

from_root
Allows you to recursively check namespaces under a child node instead of from the root of the XML doc.

返回值

The getDocNamespaces method returns an array of namespace names with their associated URIs.

范例

示例 #1 Get document namespaces

<?php

$xml = <<<XML
<?xml version="1.0" standalone="yes"?>
<people xmlns:p="http://example.org/ns">
    <p:person id="1">John Doe</p:person>
    <p:person id="2">Susie Q. Public</p:person>
</people>
XML;
 
$sxe = new SimpleXMLElement($xml);

$namespaces = $sxe->getDocNamespaces();
var_dump($namespaces);

?>

以上例程会输出:

array(1) {
   ["p"]=>
   string(21) "http://example.org/ns"
}

示例 #2 Working with multiple namespaces

<?php

$xml = <<<XML
<?xml version="1.0" standalone="yes"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
    <p:person t:id="1">John Doe</p:person>
    <p:person t:id="2" a:addr="123 Street" xmlns:a="http://example.org/addr">
        Susie Q. Public
    </p:person>
</people>
XML;
 
$sxe = new SimpleXMLElement($xml);

$namespaces = $sxe->getDocNamespaces(TRUE);
var_dump($namespaces);

?>

以上例程会输出:

array(3) {
  ["p"]=>
  string(21) "http://example.org/ns"
  ["t"]=>
  string(23) "http://example.org/test"
  ["a"]=>
  string(23) "http://example.org/addr"
}

更新日志

版本说明
5.4.0Added from_root parameter.

参见

  • SimpleXMLElement::getNamespaces
  • SimpleXMLElement::registerXPathNamespace

SimpleXMLElement::getName

Gets the name of the XML element

说明

public string SimpleXMLElement::getName ( void )

Gets the name of the XML element.

返回值

The getName method returns as a string the name of the XML tag referenced by the SimpleXMLElement object.

范例

Note:

Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.

示例 #1 Get XML element names

<?php
include 'example.php';
$sxe = new SimpleXMLElement($xmlstr);

echo $sxe->getName() . "\n";

foreach ($sxe->children() as $child)
{
    echo $child->getName() . "\n";
}

?>

以上例程会输出:

movies
movie

SimpleXMLElement::getNamespaces

Returns namespaces used in document

说明

public array SimpleXMLElement::getNamespaces ([ bool $recursive = FALSE ] )

Returns namespaces used in document

参数

recursive
If specified, returns all namespaces used in parent and child nodes. Otherwise, returns only namespaces used in root node.

返回值

The getNamespaces method returns an array of namespace names with their associated URIs.

范例

示例 #1 Get document namespaces in use

<?php

$xml = <<<XML
<?xml version="1.0" standalone="yes"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
    <p:person id="1">John Doe</p:person>
    <p:person id="2">Susie Q. Public</p:person>
</people>
XML;
 
$sxe = new SimpleXMLElement($xml);

$namespaces = $sxe->getNamespaces(true);
var_dump($namespaces);

?>

以上例程会输出:

array(1) {
  ["p"]=>
  string(21) "http://example.org/ns"
}

参见

  • SimpleXMLElement::getDocNamespaces
  • SimpleXMLElement::registerXPathNamespace

SimpleXMLElement::registerXPathNamespace

Creates a prefix/ns context for the next XPath query

说明

public bool SimpleXMLElement::registerXPathNamespace ( string $prefix , string $ns )

Creates a prefix/ns context for the next XPath query. In particular, this is helpful if the provider of the given XML document alters the namespace prefixes. registerXPathNamespace will create a prefix for the associated namespace, allowing one to access nodes in that namespace without the need to change code to allow for the new prefixes dictated by the provider.

参数

prefix
The namespace prefix to use in the XPath query for the namespace given in ns.

ns
The namespace to use for the XPath query. This must match a namespace in use by the XML document or the XPath query using prefix will not return any results.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Setting a namespace prefix to use in an XPath query

<?php

$xml = <<<EOD
<book xmlns:chap="http://example.org/chapter-title">
    <title>My Book</title>
    <chapter id="1">
        <chap:title>Chapter 1</chap:title>
        <para>Donec velit. Nullam eget tellus vitae tortor gravida scelerisque. 
            In orci lorem, cursus imperdiet, ultricies non, hendrerit et, orci. 
            Nulla facilisi. Nullam velit nisl, laoreet id, condimentum ut, 
            ultricies id, mauris.</para>
    </chapter>
    <chapter id="2">
        <chap:title>Chapter 2</chap:title>
        <para>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin 
            gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam 
            vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros 
            tellus, pharetra id, faucibus eu, dapibus dictum, odio.</para>
    </chapter>
</book>
EOD;

$sxe = new SimpleXMLElement($xml);

$sxe->registerXPathNamespace('c', 'http://example.org/chapter-title');
$result = $sxe->xpath('//c:title');

foreach ($result as $title) {
  echo $title . "\n";
}

?>

以上例程会输出:

Chapter 1
Chapter 2

Notice how the XML document shown in the example sets a namespace with a prefix of chap. Imagine that this document (or another one like it) may have used a prefix of c in the past for the same namespace. Since it has changed, the XPath query will no longer return the proper results and the query will require modification. Using registerXPathNamespace avoids future modification of the query even if the provider changes the namespace prefix.

参见

  • SimpleXMLElement::xpath
  • SimpleXMLElement::getDocNamespaces
  • SimpleXMLElement::getNamespaces

SimpleXMLElement::saveXML

别名 SimpleXMLElement::asXML

说明

此方法是该方法的别名: SimpleXMLElement::asXML

SimpleXMLElement::__toString

Returns the string content

说明

public string SimpleXMLElement::__toString ( void )

Returns text content that is directly in this element. Does not return text content that is inside this element's children.

参数

此函数没有参数。

返回值

Returns the string content on success or an empty string on failure.

范例

示例 #1 Get string content

<?php
$xml = new SimpleXMLElement('<a>1 <b>2 </b>3</a>');
echo $xml;
?>

以上例程会输出:

1 3

参见

  • SimpleXMLElement::asXML

SimpleXMLElement::xpath

Runs XPath query on XML data

说明

public array SimpleXMLElement::xpath ( string $path )

The xpath method searches the SimpleXML node for children matching the XPath path.

参数

path
An XPath path

返回值

Returns an array of SimpleXMLElement objects or FALSE in case of an error.

范例

示例 #1 Xpath

<?php
$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = new SimpleXMLElement($string);

/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

while(list( , $node) = each($result)) {
    echo '/a/b/c: ',$node,"\n";
}

/* Relative paths also work... */
$result = $xml->xpath('b/c');

while(list( , $node) = each($result)) {
    echo 'b/c: ',$node,"\n";
}
?>

以上例程会输出:

/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff

Notice that the two results are equal.

参见

  • SimpleXMLElement::registerXPathNamespace
  • SimpleXMLElement::getDocNamespaces
  • SimpleXMLElement::getNamespaces

简介

The SimpleXMLIterator provides recursive iteration over all nodes of a SimpleXMLElement object.

类摘要

SimpleXMLIterator

class SimpleXMLIterator extends SimpleXMLElement implements RecursiveIterator , Countable {

/* 方法 */

public mixed current ( void )

public SimpleXMLIterator getChildren ( void )

public bool hasChildren ( void )

public mixed key ( void )

public void next ( void )

public void rewind ( void )

public bool valid ( void )

/* 继承的方法 */

final public SimpleXMLElement::__construct ( string $data [, int $options = 0 [, bool $data_is_url = FALSE [, string $ns = "" [, bool $is_prefix = FALSE ]]]] )

public void SimpleXMLElement::addAttribute ( string $name [, string $value [, string $namespace ]] )

public SimpleXMLElement SimpleXMLElement::addChild ( string $name [, string $value [, string $namespace ]] )

public mixed SimpleXMLElement::asXML ([ string $filename ] )

public SimpleXMLElement SimpleXMLElement::attributes ([ string $ns = NULL [, bool $is_prefix = FALSE ]] )

public SimpleXMLElement SimpleXMLElement::children ([ string $ns [, bool $is_prefix = FALSE ]] )

public int SimpleXMLElement::count ( void )

public array SimpleXMLElement::getDocNamespaces ([ bool $recursive = FALSE [, bool $from_root = TRUE ]] )

public string SimpleXMLElement::getName ( void )

public array SimpleXMLElement::getNamespaces ([ bool $recursive = FALSE ] )

public bool SimpleXMLElement::registerXPathNamespace ( string $prefix , string $ns )

public string SimpleXMLElement::__toString ( void )

public array SimpleXMLElement::xpath ( string $path )

}

SimpleXMLIterator::current

Returns the current element

说明

public mixed SimpleXMLIterator::current ( void )

This method returns the current element as a SimpleXMLIterator object or NULL.

参数

此函数没有参数。

返回值

Returns the current element as a SimpleXMLIterator object or NULL on failure.

范例

示例 #1 Return the current element

<?php
$xmlIterator = new SimpleXMLIterator('<books><book>PHP basics</book><book>XML basics</book></books>');
var_dump($xmlIterator->current());

$xmlIterator->rewind(); // rewind to first element
var_dump($xmlIterator->current());
?>

以上例程会输出:

NULL
object(SimpleXMLIterator)#2 (1) {
  [0]=>
  string(10) "PHP basics"
}

参见

  • SimpleXMLIterator::key
  • SimpleXMLIterator::next
  • SimpleXMLIterator::rewind
  • SimpleXMLIterator::valid
  • SimpleXMLElement

SimpleXMLIterator::getChildren

Returns the sub-elements of the current element

说明

public SimpleXMLIterator SimpleXMLIterator::getChildren ( void )

This method returns a SimpleXMLIterator object containing sub-elements of the current SimpleXMLIterator element.

参数

此函数没有参数。

返回值

Returns a SimpleXMLIterator object containing the sub-elements of the current element.

范例

示例 #1 Return the sub-elements of the current element

<?php
$xml = <<<XML
<books>
    <book>
        <title>PHP Basics</title>
        <author>Jim Smith</author>
    </book>
    <book>XML basics</book>
</books>
XML;

$xmlIterator = new SimpleXMLIterator($xml);
for( $xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
    foreach($xmlIterator->getChildren() as $name => $data) {
    echo "The $name is '$data' from the class " . get_class($data) . "\n";
    }
}
?>

以上例程会输出:

The title is 'PHP Basics' from the class SimpleXMLIterator
The author is 'Jim Smith' from the class SimpleXMLIterator

SimpleXMLIterator::hasChildren

Checks whether the current element has sub elements

说明

public bool SimpleXMLIterator::hasChildren ( void )

This method checks whether the current SimpleXMLIterator element has sub-elements.

参数

此函数没有参数。

返回值

TRUE if the current element has sub-elements, otherwise FALSE

范例

示例 #1 Check whether the current element has sub-elements

<?php
$xml = <<<XML
<books>
    <book>
        <title>PHP Basics</title>
        <author>Jim Smith</author>
    </book>
    <book>XML basics</book>
</books>
XML;

$xmlIterator = new SimpleXMLIterator( $xml );
for( $xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
    if($xmlIterator->hasChildren()) {
        var_dump($xmlIterator->current());
    }
}
?>

以上例程会输出:

object(SimpleXMLIterator)#2 (2) {
  ["title"]=>
  string(10) "PHP Basics"
  ["author"]=>
  string(9) "Jim Smith"
}

SimpleXMLIterator::key

Return current key

说明

public mixed SimpleXMLIterator::key ( void )

This method gets the XML tag name of the current element.

参数

此函数没有参数。

返回值

Returns the XML tag name of the element referenced by the current SimpleXMLIterator object or FALSE

范例

示例 #1 Get the current XML tag key

<?php
$xmlIterator = new SimpleXMLIterator('<books><book>PHP basics</book><book>XML basics</book></books>');

echo var_dump($xmlIterator->key());
$xmlIterator->rewind(); // rewind to the first element
echo var_dump($xmlIterator->key());

?>

以上例程会输出:

bool(false)
string(4) "book"

SimpleXMLIterator::next

Move to next element

说明

public void SimpleXMLIterator::next ( void )

This method moves the SimpleXMLIterator to the next element.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 Move to the next element

<?php
$xmlIterator = new SimpleXMLIterator('<books><book>PHP Basics</book><book>XML basics</book></books>');
$xmlIterator->rewind(); // rewind to the first element
$xmlIterator->next();

var_dump($xmlIterator->current());
?>

以上例程会输出:

object(SimpleXMLIterator)#2 (1) {
  [0]=>
  string(10) "XML basics"
}

SimpleXMLIterator::rewind

Rewind to the first element

说明

public void SimpleXMLIterator::rewind ( void )

This method rewinds the SimpleXMLIterator to the first element.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 Rewind to the first element

<?php
$xmlIterator = new SimpleXMLIterator('<books><book>PHP Basics</book><book>XML Basics</book></books>');
$xmlIterator->rewind();

var_dump($xmlIterator->current());
?>

以上例程会输出:

object(SimpleXMLIterator)#2 (1) {
  [0]=>
  string(10) "PHP Basics"
}

SimpleXMLIterator::valid

Check whether the current element is valid

说明

public bool SimpleXMLIterator::valid ( void )

This method checks if the current element is valid after calls to SimpleXMLIterator::rewind or SimpleXMLIterator::next.

参数

此函数没有参数。

返回值

Returns TRUE if the current element is valid, otherwise FALSE

范例

示例 #1 Check whether the current element is valid

<?php
$xmlIterator = new SimpleXMLIterator('<books><book>SQL Basics</book></books>');

$xmlIterator->rewind(); // rewind to the first element
echo var_dump($xmlIterator->valid()); // bool(true)

$xmlIterator->next(); // advance to the next element
echo var_dump($xmlIterator->valid()); // bool(false) because there is only one element
?>