url

解析使用PHP" /> url

解析使用PHP - 源码之家" />

在此url

解析使用PHP

问题描述:

我有一个XML饲料这个XML饲料现在我试着解析的内容,特别是在<REDIRECT></REDIRECT>标签的内容。 我使用下面的代码来尝试和解析内容,但它不工作,我不知道我做错了什么。在此<a href="http://www.supashare.net/test.xml" rel="nofollow noreferrer">url</a></p> <p>解析使用PHP

$xml_file = $ADurl; 

$xml_headline_key = "*XML*RESULTS*LISTING*REDIRECT"; 
$xml_description_key = "*XML*RESULTS*LISTING*REDIRECT"; 

$story_array = array(); 

$counter = 0; 
class xml_story{ 
    var $headline, $description; 
} 

function startTag($parser, $data){ 
    global $current_tag; 
    $current_tag .= "*$data"; 
} 

function endTag($parser, $data){ 
    global $current_tag; 
    $tag_key = strrpos($current_tag, '*'); 
    $current_tag = substr($current_tag, 0, $tag_key); 
} 

function contents($parser, $data){ 
    global $current_tag, $xml_headline_key, $xml_description_key, $counter, $story_array; 
    switch($current_tag){ 
     case $xml_headline_key: 
      $story_array[$counter] = new xml_story(); 
      $story_array[$counter]->headline = $data; 
      break; 
     case $xml_description_key: 
      $story_array[$counter]->description = $data; 
      $counter++; 
      break; 
    } 
} 

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents"); 

$fp = fopen($xml_file, "r") or die("Could not open file"); 

$data = fread($fp, 1024) or die("Could not read file"); 

if(!(xml_parse($xml_parser, $data, feof($fp)))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser)); 
} 

xml_parser_free($xml_parser); 

fclose($fp); 

是不是有理由不能使用simplexml

$xml = simplexml_load_file('http://www.supashare.net/test.xml'); 
$result = $xml->xpath('/XML/RESULTS/LISTING/REDIRECT'); 
echo $result[0]; 
+0

而与此相同,它不与远程XML文件 – Imran 2009-11-20 19:26:59

+0

打开错误看看会发生什么工作。您的服务器可能没有启用allow_url_fopen,在这种情况下,您将不得不编辑您的php.ini(如果可能)以启用它。 – Galen 2009-11-20 19:49:50

+0

if simplexml_load_file will not work then replace it with this .... $ xml = simplexml_load_string(file_get_contents($ url)); 其中$ url是你想要的网址 – ChronoFish 2009-11-20 19:50:01

$xml = stream_get_contents($fp); 
$xml = new SimpleXMLElement($xml); 
echo $xml->RESULTS->LISTING->REDIRECT; 
+0

那么问题是,它无法解析远程XML文件。 – Imran 2009-11-20 19:26:19

+0

你是什么意思?你的服务器本地有XML文件吗?如果是这样,请将URL更改为该文件名,它将起作用。 – 2009-11-20 19:28:30

+0

不,xml文件位于另一台服务器上的另一台服务器上。 – Imran 2009-11-20 19:29:20

$doc = new DomDocument(); 
$doc->load('http://www.supashare.net/test.xml'); 
$q = new DomXPath($doc); 
echo $q->query('//REDIRECT')->item(0)->nodeValue; 
+0

我记得你从站点,你是一个我喜欢阅读的职位的人。你能告诉我为什么你选择domdocument而不是simplexml吗? – Galen 2009-11-20 19:26:50

+0

嗯..我觉得你用别的东西混淆了我:S 至于你的答案..它不能与远程XML文件一起工作 – Imran 2009-11-20 19:48:38

+0

它们使用相同的底层解析(libxml),所以它只是语法。我个人更喜欢DOM,因为它是一个标准API。这只是一种偏好。 – troelskn 2009-11-20 20:05:37