php - 为什么这个 xmlreader 代码不起作用?

标签 php xml xml-parsing xmlreader

我有一个如下所示的文件:

    <ExternalPage about="http://animation.about.com/">
       <d:Title>About.com: Animation Guide</d:Title>
       <d:Description>Keep up with developments in online animation for all skill levels.     Download tools, and seek inspiration from online work.</d:Description>
       <topic>Top/Arts/Animation</topic>
    </ExternalPage>
    <ExternalPage about="http://www.toonhound.com/">
       <d:Title>Toonhound</d:Title>
       <d:Description>British cartoon, animation and comic strip creations - links, reviews  and news from the UK.</d:Description>
       <topic>Top/Arts/Animation</topic>
    </ExternalPage>

等等

我正在尝试获取“about”网址,以及嵌套的标题和描述。我已经尝试了以下代码,但我得到的只是一堆破折号......

$reader = new XMLReader();

if (!$reader->open("dbpedia/links/xml.xml")) {
die("Failed to open 'xml.xml'");
}
$num=0;
while($reader->read() && $num<200) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'ExternalPage') {
$url = $reader->getAttribute('about');

while ($xml->nodeType !== XMLReader::END_ELEMENT ){
$reader->read();

 if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'd:Title') {
 $title=$xmlReader->value;
 }
elseif ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'd:Description') {
$desc=$xmlReader->value;
}
}

}
$num++;echo $url."-".$title."-".$desc."<br />";
}
$reader->close();

我是 xmlreader 的新手,所以如果有人能找出我做错了什么,我将不胜感激。

注意:我使用 xmlreader 因为该文件很大(数百万行)。

编辑:文件的开头如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<RDF xmlns:r="http://www.w3.org/TR/RDF/" xmlns:d="http://purl.org/dc/elements/1.0/"       xmlns="http://dmoz.org/rdf/">
  <!-- Generated at 2013-02-10 00:03:45 EST from DMOZ 2.0 -->
  <Topic r:id="">
<catid>1</catid>
  </Topic>
<Topic r:id="Top/Arts">
    <catid>381773</catid>
  </Topic>
  <Topic r:id="Top/Arts/Animation">
  <catid>423945</catid>
<link1 r:resource="http://www.awn.com/"></link1>
<link r:resource="http://animation.about.com/"></link>
<link r:resource="http://www.toonhound.com/"></link>
<link r:resource="http://enculturation.gmu.edu/2_1/pisters.html"></link>
<link r:resource="http://www.digitalmediafx.com/Features/animationhistory.html"></link>
<link r:resource="http://www.spark-online.com/august00/media/romano.html"></link>
<link r:resource="http://www.animated-divots.net/"></link>
</Topic>
<ExternalPage about="http://www.awn.com/">
<d:Title>Animation World Network</d:Title>
<d:Description>Provides information resources to the international animation community. Features include searchable database archives, monthly magazine, web animation guide, the Animation Village, discussion forums and other useful resources.</d:Description>
<priority>1</priority>
<topic>Top/Arts/Animation</topic>
</ExternalPage>

等等

最佳答案

需要时间和适当的调试才能得到可工作的纯 XMLReader 代码。同时尝试这种混合方法:

$xmlR = new XMLReader;
$xmlR->open('dbpedia/links/xml.xml');

//Skip until <ExternalPage> node
while ($xmlR->read() && $xmlR->name !== 'ExternalPage');

$loadedNS_f = false;
while ($xmlR->name === 'ExternalPage')
{
    //Read the entire parent tag with children
    $sxmlNode = new SimpleXMLElement($xmlR->readOuterXML());

    //collect all namespaces in node recursively once; assuming all nodes are similar
    if (!$loadedNS_f) {
        $tagNS = $sxmlNode->getNamespaces(true);
        $loadedNS_f = true; 
    }
    $URL = (string) $sxmlNode['about'];
    $dNS = $sxmlNode->children($tagNS['d']);
    $Title = (string) $dNS->Title;
    $Desc = (string) $dNS->Description;
    $Topic = (string)$sxmlNode->topic;

    var_dump($URL, $Title, $Desc, $Topic);

    // Jump to next <ExternalPage> tag
    $xmlR->next('ExternalPage');
}

$xmlR->close();

关于php - 为什么这个 xmlreader 代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14884440/

相关文章:

从 XML 字段作为多条记录进行 SQL SELECT

sql - 使用内联查询从 SSRS 数据集中提取数据库对象依赖项(查询类型 : Text)

java - 使用 Java 解析 ALM XML 数据

php - 无法从禁用的单选按钮中获取 $_POST 值

php - XML/Parser.php 安装

javascript - 带有 XML 和按钮的动态 HTML5 播放器

java - 根据提供的条件获取 xml 中的特定对象

python - XML 解析问题,某些孟加拉语字符在 Python ElementTree 中显示 ParseError

php - 当输入通过按键聚焦时,我想通过 ajax 发送一个值

php - YouTube API : Displaying thumbnails of each video. ..?