Foros del Web » Programación para mayores de 30 ;) » Programación General »

Ayuda para integrar XML con parser PHP

Estas en el tema de Ayuda para integrar XML con parser PHP en el foro de Programación General en Foros del Web. 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> ...
  #1 (permalink)  
Antiguo 05/05/2004, 09:32
 
Fecha de Ingreso: febrero-2003
Mensajes: 67
Antigüedad: 21 años, 2 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 "";
?>
  #2 (permalink)  
Antiguo 05/05/2004, 13:14
Avatar de cvander
Moderador
 
Fecha de Ingreso: abril-2001
Ubicación: Ciudadano del mundo
Mensajes: 13.638
Antigüedad: 23 años
Puntos: 1792
Revisa:

http://www.forosdelweb.com/showthrea...hreadid=191204
__________________
- Christian Van Der Henst
Platzi
  #3 (permalink)  
Antiguo 08/05/2004, 04:12
 
Fecha de Ingreso: febrero-2003
Mensajes: 67
Antigüedad: 21 años, 2 meses
Puntos: 0
Gracias.
Los enlaces me han ayudado pero no termino de conseguir hacer funcionar correctamente al parser.

Consigo extraer los datos del archivo XML y mostrarlos aunque no de forma correcta.

Quizas sea porque existen unos parámetros que no entiendo del script:

numberofa ???
maxa ???

Finalmente los datos que recojo los he dejado así:
PRINTED BEFORE
Código:
switch ($name) {
      case "URI":
	     print "<a href='"; 
	     $write = "true"; /* This will print the result */
                     break;
     
      case "URL":
	     print "";
	     $write = "true"; /* This will print the Title */
	     break;	
      case "DESCRIPTION":
	     print "";
	     $write = "true"; /* This will print the Description */
	     break;
PRINTED AFTER
Código:
switch ($name) {
      case "URI":
	    print "'>";
	     $write = "true";
         break;  
      case "URL":
	     print "</a><br>";
	     $write = "true";
	     break;	
      case "DESCRIPTION":
	     print "<br><br>";
	     $write = "true";
	     break;
Pero, se me muestran unos números delante de la URL que no deseo y se muestran tanto la descripción del item anterior como la URL del item siguiente como enlace de texto unido.
¿Porqué?

Ya puestos, me interesaría añadir a las URI las variables:

Código:
ONMOUSEOVER="window.status='http://...';return true" ONFOCUS="window.status='http://...;return true" ONMOUSEOUT="window.status='...'''
y no se como hacerlo.

Una ayudita y no os molesto más. Perdonad mi ignorancia.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:33.