如何使用php从xml文件中读取特定节点
这是我试图获取的XML文件的结构如何使用php从xml文件中读取特定节点,php,Php,这是我试图获取的XML文件的结构 <feed> <entry> <id>1347030</id> <title>abcd</title> <description>xyz</description> <brand>mnop</brand> <link>http://www.abc
<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>
1347030
abcd
xyz
mnop
http://www.abcd.com/1.html
http://www.abcd.com/1.jpg
这是XML的一个小片段…文件很大…所以我使用XMLReader
我想去拿行李
图像链接
及
链接
来自此XML提要的节点。为了实现这一点,我完成了以下代码
$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();
}
}
$xmlDocument=“test.xml”;
$xml=newXMLReader();
$xml->open($xmlDocument);
而($xml->read()){
如果($xml->name==“图像链接”){
echo$xml->link.“
”;
回声“
”;
$xml->next();
}
}
这将返回图像,但未获取链接节点…有什么建议如何执行此操作吗
<?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();
}
}
?>
假设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>
1347030
abcd
xyz
mnop
http://www.abcd.com/1.html
http://www.abcd.com/1.jpg
我将按以下方式使用数据提取库:
<?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());
}
@Vladimir…感谢您的及时回复,但我不能使用DOMDocument…因为文件较大,我只使用XMLReader。我还必须在这里使用一些循环…添加了多个记录处理/循环。我在使用XMLReader时有过不好的经验/局限性,所以DOMDocument是我首选的xml文件遍历方法。@Vladimir…谢谢,但对于较大的文件,DOMDocument显示出一些错误回声“
”;这将返回空结果,但已更新为使用XMLReader。让我知道这是否解决了你的问题。