开发者

Create an array to input multiple urls instead of the one I have I have now

开发者 https://www.devze.com 2023-03-17 08:49 出处:网络
How can I modify the following code to get instead of the url of one xml, an array with multiple urls? The code below is working properly but as I said for one file.

How can I modify the following code to get instead of the url of one xml, an array with multiple urls? The code below is working properly but as I said for one file.

<?php

$dom = new DOMDocument;
$dom->loadHTMLFile ('domain.com/xmlfile.xml');

$xPath = new DOMXPath($dom);

$posts = $xPath->query('//page');

foreach($posts as $post) {

$fans = $post->getElementsByTagName( "fan_count" )->item(0)->nodeValue;
echo $开发者_Python百科fans;

}

?>


You'd have to use a foreach loop.

<?php

$files = array('file1.xml', 'file2.xml', 'file3.xml');

foreach ( $files as $file ) {
  $dom = new DOMDocument;
  $dom->loadHTMLFile($file);

  $xPath = new DOMXPath($dom);

  $posts = $xPath->query('//page');

  foreach($posts as $post) {
    $fans = $post->getElementsByTagName('fan_count')->item(0)->nodeValue;

    echo $fans;
  }
}

?>


You'd need to loop over each file and extract the urls from each in turn. A DOMDocument object can handle only one single XML tree. Multiple XML files implies multiple document roots, which is not permitted.

I suppose you could create a "meta" XML document and insert each seperate file as a node off the meta XML's root node. Either way, slightly painful.


$arr = array(0 => "url1", 1 => "url1");

for($i=0;$i<2;$i++)
{


$dom = new DOMDocument;
$dom->loadHTMLFile ($arr[$i]);

$xPath = new DOMXPath($dom);

$posts = $xPath->query('//page');

foreach($posts as $post) {

$fans = $post->getElementsByTagName( "fan_count" )->item(0)->nodeValue;
echo $fans;

}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消