0

请检查此代码,取自此处

 <?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";
        }
    }
    ?>

如何使用迭代器从所有书籍节点附加“XML基础知识”(在第二本书节点中)获取值?

4

1 回答 1

1

这将为您XML basics提供输出。

<?php
    $xml = <<<XML
    <books>
        <book>
            <title>PHP Basics</title>
            <author>Jim Smith</author>
        </book>
        <book>XML basics</book>
    </books>
    XML;
for( $xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
        $xmlIterator->next();
        echo $xmlIterator->getChildren();
    }
?>

[:edit] 它将显示两本书

$xmlIterator = new SimpleXMLIterator($xml);
    for( $xmlIterator->rewind(); $xmlIterator->valid(); $xmlIterator->next() ) {
        foreach($xmlIterator->getChildren() as $name => $data) {
        echo "$data<br />";
        break;
        }
        echo $xmlIterator->getChildren();
    }
于 2013-09-28T00:37:44.743 回答