开发者

how to process an XML NODE in PHP

here is the simple code. i just want 开发者_运维问答to know how to process the node that is being returned by $reader->expand();

<?php
$reader = new XMLReader();
if (!$reader->open("data.xml"))
{
    die("Failed to open 'data.xml'");
}
while($reader->read())
{
    $node = $reader->expand();
    // just want to know how to process this node now?
}
$reader->close();
?>


<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<parent xmlns:dc="myNS"> 
<dc:child> <dc:a>A</dc:a> <dc:b>B</dc:b> </dc:child> 
<dc:child> <dc:a>c</dc:a> <dc:b>d</dc:b> </dc:child> 
<dc:child> <dc:a>e</dc:a> <dc:b>f</dc:b> </dc:child> 
<dc:child> <dc:a>g</dc:a> <dc:b>h</dc:b> </dc:child> 
</parent> 
XML;

$reader = new XMLReader();
$reader->xml($xml);
while($reader->read()) {
    switch($reader->nodeType) {
        case constant('XMLREADER::ELEMENT'):
            if ($reader->name == 'dc:child') {
                //insert into db
                var_dump($reader->readInnerXml());
            }
            break;
    }
}

$reader->close();

Output:

string(63) " <dc:a xmlns:dc="myNS">A</dc:a> <dc:b xmlns:dc="myNS">B</dc:b> "
string(63) " <dc:a xmlns:dc="myNS">c</dc:a> <dc:b xmlns:dc="myNS">d</dc:b> "
string(63) " <dc:a xmlns:dc="myNS">e</dc:a> <dc:b xmlns:dc="myNS">f</dc:b> "
string(63) " <dc:a xmlns:dc="myNS">g</dc:a> <dc:b xmlns:dc="myNS">h</dc:b> "
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜