ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-07 09:32:03  阅读:186  来源: 互联网

标签:xmlreader php xml xml-parsing


我有一个看起来像这样的文件:

    <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>

等等

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

$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();

标签:xmlreader,php,xml,xml-parsing
来源: https://codeday.me/bug/20191007/1866117.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有