如何使用php从xml文件中读取特定节点

时间:2015-03-08 19:58:35

标签: php

以下是我试图获取的XML文件的结构...

<feed>
    <entry>
        <id>1347030</id>
        <title>abcd</title>
        <description>xyz</description>
        <brand>mnop</brand>
        <link>http://www.abcd.com/1.html</link>
        <image_link>http://www.abcd.com/1.jpg</image_link>
     </entry>
</feed>

这是XML的一小段......文件很大......所以我正在使用XMLReader

我想要获取

  

图片链接

  

链路

来自此XML Feed的

节点。为此,我完成了以下代码。

$xmlDocument = "test.xml";
$xml = new XMLReader();
$xml->open($xmlDocument);
while( $xml->read() ) {
if($xml->name == "image_link") {
    echo $xml->link ."<br/>";
  echo "<a href='".$xml->link."'><img height='80' width='100' src=" .$xml->readInnerXML()."></a><br />";
$xml->next();
}
}

这会返回我的图像,但未获取链接节点....任何建议如何执行此操作

2 个答案:

答案 0 :(得分:0)

<?php
    $doc = new DOMDocument; 
    $doc->load('in2.xml');
    echo $doc->saveXML();
    $sImageLink = $doc->getElementsByTagName( 'image_link' )->item( 0 )->nodeValue;
    $sLink      = $doc->getElementsByTagName( 'link' )->item( 0 )->nodeValue;

    var_dump( $sImageLink );
    var_dump( $sLink );
?>

对于多个记录:

<?php
    $doc = new DOMDocument; 
    $doc->load('in2.xml');
    // echo $doc->saveXML();

    $oEntries = $doc->getElementsByTagName( 'entry' );
    for( $i = 0; $i < $oEntries->length; ++$i )
    {
        $aImageLinks[] = $doc->getElementsByTagName( 'image_link' )->item( $i )->nodeValue;
        $aLinks[]      = $doc->getElementsByTagName( 'link' )->item( $i )->nodeValue;
    }
    var_dump( $aImageLinks );
    var_dump( $aLinks );
    // file_put_contents( 'in2.xml', $doc->saveXML() );
?>

使用XMLReader:

<?php
    $sInFile = 'in2.xml';
    $oXml = new XMLReader();
    $oXml->open( $sInFile );

    while( $oXml->read() ) 
    {
        if( $oXml->name == 'link' ) 
        {
            echo $oXml->readInnerXML();
            echo '<br/>';
        }
        if( $oXml->name == 'image_link' ) 
        {
            echo '<a href="';
            echo $oXml->link;
            echo '"><img height="80" width="100" src="';
            echo $oXml->readInnerXML();
            echo '"></a><br />';
            $oXml->next();
        }
    }
?>

答案 1 :(得分:0)

假设XML文件具有以下结构:

<?xml version="1.0" encoding="UTF-8"?>
<feeds>
    <feed>
        <entry>
            <id>1347030</id>
            <title>abcd</title>
            <description>xyz</description>
            <brand>mnop</brand>
            <link>http://www.abcd.com/1.html</link>
            <image_link>http://www.abcd.com/1.jpg</image_link>
         </entry>
    </feed>
    <!-- more feed elements continue -->
</feeds>

我会以下列方式使用Essence数据提取库:

<?php

require 'vendor/autoload.php';

use Impensavel\Essence\XMLEssence;
use Impensavel\Essence\EssenceException;

$config = array(
    '/feeds/feed/entry' => array(
        'map'      => array(
            'image' => 'string(image_link)',
            'link'  => 'string(link)',
        ),
        'callback' => function ($data) {            
            echo sprintf('<a href="%s"><img height="80" width="100" src="%s"></a><br />', $data['properties']['link'], $data['properties']['image']);
        },
    ),
);

try
{
    $essence = new XMLEssence($config);

    $essence->extract(new SplFileInfo('feeds.xml'));

} catch (EssenceException $e) {
    var_dump($e->getMessage());
}

这个库可以轻松处理非常大的XML文件(我已经将它用于超过1GB的文件)。