问题描述
我一直在用PHP解析XML时遇到问题,却没有真正找到正确的方式"或至少没有一种标准化的解析XML文件的方式.
I've consistently had an issue with parsing XML with PHP and not really found "the right way" or at least a standardised way of parsing XML files.
首先,我正在尝试对此进行解析:
Firstly i'm trying to parse this:
<item>
<title>2884400</title>
<description><![CDATA[ ><img width="126" alt="" src="http://userserve-ak.last.fm/serve/126/27319921.jpg" /> ]]></description>
<link>http://www.last.fm/music/+noredirect/Beatles/+images/27319921</link>
<author>anne710</author>
<pubDate>Tue, 21 Apr 2009 16:12:31 +0000</pubDate>
<guid>http://www.last.fm/music/+noredirect/Beatles/+images/27319921</guid>
<media:content url="http://userserve-ak.last.fm/serve/_/27319921/Beatles+2884400.jpg" fileSize="13065" type="image/jpeg" expression="full" width="126" height="126" />
<media:thumbnail url="http://userserve-ak.last.fm/serve/126/27319921.jpg" type="image/jpeg" width="126" height="126" />
</item>
我正在使用以下代码:
$doc = new DOMDocument();
$doc->load('http://ws.audioscrobbler.com/2.0/artist/beatles/images.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
现在我想获取"media:content"和"media:thumbnail" URL属性,我该怎么做?现在我认为我应该使用DOMElement :: getAttribute,但是我没有设法使其起作用:/任何人都可以对此有所了解,也请让我知道这是否是解析XML的好方法吗?
Now I want to get the "media:content" and "media:thumbnail" url attributes, how would i do that? Now i think i should be using DOMElement::getAttribute but i haven't managed to get it to work :/ Can anyone shed some light on this, and also let me know if this is a good way to parse XML?
关于,沙迪
推荐答案
这就是我最终使用XMLReader完成的方式:
This was how i have eventually done it using XMLReader:
<?php
define ('XMLFILE', 'http://ws.audioscrobbler.com/2.0/artist/vasco%20rossi/images.rss');
echo "<pre>";
$items = array ();
$i = 0;
$xmlReader = new XMLReader();
$xmlReader->open(XMLFILE, null, LIBXML_NOBLANKS);
$isParserActive = false;
$simpleNodeTypes = array ("title", "description", "media:title", "link", "author", "pubDate", "guid");
while ($xmlReader->read ())
{
$nodeType = $xmlReader->nodeType;
// Only deal with Beginning/Ending Tags
if ($nodeType != XMLReader::ELEMENT && $nodeType != XMLReader::END_ELEMENT) { continue; }
else if ($xmlReader->name == "item") {
if (($nodeType == XMLReader::END_ELEMENT) && $isParserActive) { $i++; }
$isParserActive = ($nodeType != XMLReader::END_ELEMENT);
}
if (!$isParserActive || $nodeType == XMLReader::END_ELEMENT) { continue; }
$name = $xmlReader->name;
if (in_array ($name, $simpleNodeTypes)) {
// Skip to the text node
$xmlReader->read ();
$items[$i][$name] = $xmlReader->value;
} else if ($name == "media:thumbnail") {
$items[$i]['media:thumbnail'] = array (
"url" => $xmlReader->getAttribute("url"),
"width" => $xmlReader->getAttribute("width"),
"height" => $xmlReader->getAttribute("height"),
"type" => $xmlReader->getAttribute("type")
);
} else if ($name == "media:content") {
$items[$i]['media:content'] = array (
"url" => $xmlReader->getAttribute("url"),
"width" => $xmlReader->getAttribute("width"),
"height" => $xmlReader->getAttribute("height"),
"filesize" => $xmlReader->getAttribute("fileSize"),
"expression" => $xmlReader->getAttribute("expression")
);
}
}
print_r($items);
echo "</pre>";
?>
这篇关于使用PHP解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!