Jump to content


Photo

Xml_parse - Erro Na Leitura De 2 Urls


  • Faça o login para participar
2 replies to this topic

#1 kboing

kboing

    Normal

  • Usuários
  • 77 posts
  • Sexo:Não informado

Posted 03/06/2008, 18:33

fiz um script utilizando "xml_parse" para ler 2 arquivos xml, mas apos ler o primeiro arquivo dá o erro "PHP Fatal error: Cannot redeclare startelement()" e não lê o segundo arquivo.

como fazer para ler os dois arquivos?

SCRIPT:


<?PHP

$xmlurls = array(
"file1.xml",
"file2.xml"
);

for($i = 0; $url = $xmlurls[$i]; $i++) {

$insideitem = false;
$item = array();
$tag = "";
$title = "";
$link = "";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $link, $item;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $title, $link, $item, $attrs;
if ($name == "ITEM") {
echo "$title : $link";
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $title, $link, $pubdate, $description, $item, $attrs;
if ($insideitem) {
switch ($tag) {
case "ITEM":
$id .= $item['ID'];
break;
case "TITLE":
$title .= $data;
break;
case "LINK":
$link .= $data;
}
}
}


$xml_parser = xml_parser_create("");
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("$url","r")
or die("Error reading data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

}

?>


se alguem puder ajudar, agradeço.

#2 victorhb

victorhb

    24 Horas

  • Usuários
  • 489 posts
  • Sexo:Masculino
  • Localidade:Brasília-DF

Posted 03/06/2008, 19:35

Você tá declarando as funções dentro do loop, tire elas de dentro, e apenas chame quando for necessário.

#3 kboing

kboing

    Normal

  • Usuários
  • 77 posts
  • Sexo:Não informado

Posted 03/06/2008, 21:14

valeu,

agora funcionou

Edição feita por: kboing, 03/06/2008, 21:30.





0 user(s) are reading this topic

0 membro(s), 0 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq