Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/05/2004, 09:32
wood
 
Fecha de Ingreso: febrero-2003
Mensajes: 67
Antigüedad: 21 años, 8 meses
Puntos: 0
Ayuda para integrar XML con parser PHP

Estoy muy verde en estos temas.
Necesito integrar el siguiente formato XML, para que aparezcan el title, Description y URL:

<Listings>
<Page></Page>
<Count></Count>
<Listing>
<Title></Title>
<URL></URL>
<URI></URI>
<Description></Description>
<Bid></Bid>

</Listing>
<Listing>
<Title></Title>
<URL></URL>
<URI></URI>
<Description> </Description>
<Bid></Bid>
</Listing>
...
</Listings>

Dispongo de este parser PHP, aunque pudiera ser otro:

Código:
<?php
/if ($current_category["pages"] == "y")
$search = "".$current_category["name"]; /* Term to be searched */
 


/* ---------- Ignore this part ---------- */
$_allow_ip = array( '*.*.*.*' );
$_ip = $_SERVER['REMOTE_ADDR'];

$file = "http://www.url.com/rd/feed/XMLFeed.jsp?trackID=xxxxxxx&pID=xxxxx&cat=&searchnl=5&page=1&ip=1.1.1.1&excID=";
$write = "true";
$numberofa = 0;
$maxa = 10;

/* Everything between the "" in this section will be printed BEFORE the result */
function startElement($parser, $name, $attrs) {
global $write;
global $numberofa;
global $maxa;
if ($numberofa < $maxa) {
   switch ($name) {
      case "TYPE":
	     print ""; /* So anything here would show up before the TYPE Result */
	  	 $write = "false"; 
      break;
      case "REDIRECT_URL":
	     print "<a href='"; 
	     $write = "true"; 
      break;
      case "SITE_URL":  
	     print "";
		   $write = "false"; 
      break;
      case "DOMAIN":
	     print "";
	     $write = "false"; 
      break;
      case "TITLE":
	     print "<b>";
	     $write = "true"; /* This will print the Title */
	     break;	
      case "DESCRIPTION":
	     print "";
	     $write = "true"; /* This will print the Description */
	     break;
			 case "LOGOLINK":
	     print "";
	     $write = "false";  /* This will print the logo (if any), however I have no work around for no logo yet */
	     break;
			 case "BID":
	     print "";
	     $write = "false";  /* This will print the bid price */
	     break;
			 case "NK":
	     print "";
	     $write = "false";  /* I forget what this is... */
	     break;
   }
}
}


/* Everything between the "" in this section will be printed AFTER the result */
function endElement($parser, $name) {
global $write;
global $numberofa;
global $maxa;
			
if ($numberofa < $maxa) {
  switch ($name) {
      case "TYPE":
	    print ""; /* So anything here would show up after the TYPE Result */
	  	 $write = "false";
 	     $numberofa++;
	  	 break;
      case "REDIRECT_URL":
	    print "'>";
	     $write = "true";
      break;
      case "SITE_URL":
	     print "";
		   $write = "false";
	     break;
      case "DOMAIN":
	     print "";
	     $write = "false";
	     break;
      case "TITLE":
	     print "</b></a><br>";
	     $write = "true";
	     break;	
      case "DESCRIPTION":
	     print "<br><br>";
	     $write = "true";
	     break;
			 case "LOGOLINK":
	     print "";
	     $write = "false";
	     break;
			 case "BID":
	     print "";
	     $write = "false";
	     break;
			 case "NK":
	     print "";
	     $write = "false";
	     break;
   }
}
}

/* ---------- Nothing to edit beyond this point ---------- */

function characterData($parser, $data) {
global $write;
if ($write == "true") {
   print $data;
}

}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
   die("No more data");
}
while ($data = fread($fp, 4096)) {
   if (!xml_parse($xml_parser, $data, feof($fp))) {
      die(sprintf("XML error: %s at line %d",
      xml_error_string(xml_get_error_code($xml_parser)),
      xml_get_current_line_number($xml_parser)));
   }
}
xml_parser_free($xml_parser);
echo "";
?>