Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Simple Xml php5

Estas en el tema de Simple Xml php5 en el foro de PHP en Foros del Web. Hola como estan estoy queriendo leer un xml con simplexml pero me da este error Fatal error: Uncaught exception 'Exception' with message 'String could not ...
  #1 (permalink)  
Antiguo 15/01/2014, 09:00
 
Fecha de Ingreso: enero-2012
Mensajes: 224
Antigüedad: 12 años, 3 meses
Puntos: 1
Simple Xml php5

Hola como estan estoy queriendo leer un xml con simplexml pero me da este error



Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML


que estoy haciendo mal este es mi codigo

Código PHP:
<?php 
$data 
"http://alerts.weather.gov/cap/tx.php?x=1";
$library file_get_contents($data);
$library = new SimpleXMLElement($library);


echo 
'<table>';
echo 
'<tr>';
echo   
'<th>Title</th>
        <th>Author</th>
        <th>Publisher</th>
        <th>ISBN</th>'
;
echo 
'</tr>';
foreach( 
$library->book as $book ) {
    echo 
'<tr>';
    echo 
'<td>' $book->entry '</td>';
    echo 
'<td>' $book->published '</td>';
    echo 
'<td>' $book->name '</td>';
    echo 
'<td>' $book->title '</td>';
    echo 
'</tr>';
}
echo 
'</table>';


?>
  #2 (permalink)  
Antiguo 15/01/2014, 09:12
Avatar de jonni09lo
Colaborador
 
Fecha de Ingreso: septiembre-2011
Ubicación: Estigia
Mensajes: 1.471
Antigüedad: 12 años, 7 meses
Puntos: 397
Respuesta: Simple Xml php5

Y si pruebas esto

Código PHP:
Ver original
  1. <?php
  2. $opciones = array(
  3.     'http'=>array(
  4.         'method'=>"GET",
  5.         'header'=>"Content-type: text/xml\r\n",
  6.     )
  7. );
  8. $contexto = stream_context_create($opciones);
  9. $data = "http://alerts.weather.gov/cap/tx.php?x=1";
  10. $library = file_get_contents($data, false, $contexto);
  11. $library = new SimpleXMLElement($library);

Saludos
__________________
Haz preguntas inteligentes-Como ser Hacker
No hacer preguntas por mensaje privado. No sólo no es inteligente sino que es egoísta.
  #3 (permalink)  
Antiguo 15/01/2014, 09:21
 
Fecha de Ingreso: enero-2012
Mensajes: 224
Antigüedad: 12 años, 3 meses
Puntos: 1
Respuesta: Simple Xml php5

hola me da este error, la linea 103 es esta

$library = new SimpleXMLElement($library);

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/baldia/public_html/wp-content/themes/brownaldia/index.php:103 Stack trace: #0 /home/baldia/public_html/wp-content/themes/brownaldia/index.php(103): SimpleXMLElement->__construct('') #1 /home/baldia/public_html/wp-includes/template-loader.php(74): include('/home/baldia/pu...') #2 /home/baldia/public_html/wp-blog-header.php(16): require_once('/home/baldia/pu...') #3 /home/baldia/public_html/index.php(17): require('/home/baldia/pu...') #4 {main} thrown in /home/baldia/public_html/wp-content/themes/brownaldia/index.php on line 103
  #4 (permalink)  
Antiguo 15/01/2014, 09:28
Avatar de jonni09lo
Colaborador
 
Fecha de Ingreso: septiembre-2011
Ubicación: Estigia
Mensajes: 1.471
Antigüedad: 12 años, 7 meses
Puntos: 397
Respuesta: Simple Xml php5

Usa simplexml_load_string

Código PHP:
Ver original
  1. $library = simplexml_load_string($library);

Si no funciona significa que la pagina a la que estas accediendo no te esta retornado un XML. Cual es la url del sitio? (http://alerts.weather.gov/cap/tx.php?x=1 no devuleve un XML, devuelve un HTML)

Saludos
__________________
Haz preguntas inteligentes-Como ser Hacker
No hacer preguntas por mensaje privado. No sólo no es inteligente sino que es egoísta.
  #5 (permalink)  
Antiguo 15/01/2014, 09:37
 
Fecha de Ingreso: enero-2012
Mensajes: 224
Antigüedad: 12 años, 3 meses
Puntos: 1
Respuesta: Simple Xml php5

en realidad el xml que necesito parsear es este

http://www.eldolarblue.net/getDolarBlue.php?as=xml


con lo ultimo que me dijiste logre que no me de el error pero no me arroja ningun resultado
  #6 (permalink)  
Antiguo 15/01/2014, 09:43
Avatar de jonni09lo
Colaborador
 
Fecha de Ingreso: septiembre-2011
Ubicación: Estigia
Mensajes: 1.471
Antigüedad: 12 años, 7 meses
Puntos: 397
Respuesta: Simple Xml php5

Según tu código quieres traer libros y la pagina devuelve una estructura base de eldolarbluedotnet

Saludos
__________________
Haz preguntas inteligentes-Como ser Hacker
No hacer preguntas por mensaje privado. No sólo no es inteligente sino que es egoísta.
  #7 (permalink)  
Antiguo 15/01/2014, 09:55
 
Fecha de Ingreso: enero-2012
Mensajes: 224
Antigüedad: 12 años, 3 meses
Puntos: 1
Respuesta: Simple Xml php5

Es verdad gracias

ahora intente asi pero sigue sin darme resultados, muchas gracias por tu aporte

Código PHP:
<?php 


$opciones 
= array(
    
'http'=>array(
        
'method'=>"GET",
        
'header'=>"Content-type: text/xml\r\n",
    )
);
$contexto stream_context_create($opciones);
$data "http://www.eldolarblue.net/getDolarBlue.php?as=xml"
$library file_get_contents($datafalse$contexto); 
$library simplexml_load_string($library);






echo 
'<table>';
echo 
'<tr>';
echo   
'<th>Compra</th>
        <th>Venta</th>'
;
echo 
'</tr>';
foreach( 
$library->eldolarbluedotnet as $dolar ) {
    echo 
'<tr>';
    echo 
'<td>' $dolar->buy '</td>';
    echo 
'<td>' $dolar->sell '</td>';
  

    echo 
'</tr>';
}
echo 
'</table>';


?>

de este xml


Código:
<?xml version='1.0' encoding='UTF-8'?><eldolarbluedotnet>
<url>http://www.eldolarblue.net/</url><datetime>2014-01-15 11:30:03</datetime><exchangerate>
<buy>10.8800</buy><sell>10.9300</sell></exchangerate></eldolarbluedotnet>
  #8 (permalink)  
Antiguo 15/01/2014, 10:13
Avatar de jonni09lo
Colaborador
 
Fecha de Ingreso: septiembre-2011
Ubicación: Estigia
Mensajes: 1.471
Antigüedad: 12 años, 7 meses
Puntos: 397
Respuesta: Simple Xml php5

Aparentemente esa pagina siempre retornará un valor asi que puedes hacer esto:

Código PHP:
Ver original
  1. $library = (array) simplexml_load_string($library );
  2. $dolar = (array) $library['exchangerate'];
  3. echo '<table>';
  4.     echo '<tr>';
  5.         echo '<th>Compra</th>
  6.         <th>Venta</th>';
  7.     echo '</tr>';
  8.     echo '<tr>';
  9.         echo '<td>' . $dolar['buy'] . '</td>';
  10.         echo '<td>' . $dolar['sell']. '</td>';
  11.     echo '</tr>';
  12. echo '</table>';

Saludos
__________________
Haz preguntas inteligentes-Como ser Hacker
No hacer preguntas por mensaje privado. No sólo no es inteligente sino que es egoísta.
  #9 (permalink)  
Antiguo 15/01/2014, 10:19
 
Fecha de Ingreso: enero-2012
Mensajes: 224
Antigüedad: 12 años, 3 meses
Puntos: 1
Respuesta: Simple Xml php5

El codigo final me queda asi, de todas formas me devuelve vacio

no entiendo por que usaste array , la verdad ya me perdi un poco pero no puedo obtener los datos que necesito lamentablemente

Código PHP:


<?php 


$opciones 
= array(
    
'http'=>array(
        
'method'=>"GET",
        
'header'=>"Content-type: text/xml\r\n",
    )
);
$contexto stream_context_create($opciones);
$data "http://www.eldolarblue.net/getDolarBlue.php?as=xml"
$library file_get_contents($datafalse$contexto); 
$library = (array) simplexml_load_string($library );
$dolar = (array) $library['exchangerate'];
echo 
'<table>';
    echo 
'<tr>';
        echo 
'<th>Compra</th>
        <th>Venta</th>'
;
    echo 
'</tr>';
    echo 
'<tr>';
        echo 
'<td>' $dolar['buy'] . '</td>';
        echo 
'<td>' $dolar['sell']. '</td>';
    echo 
'</tr>';
echo 
'</table>';

?>
  #10 (permalink)  
Antiguo 15/01/2014, 10:27
Avatar de jonni09lo
Colaborador
 
Fecha de Ingreso: septiembre-2011
Ubicación: Estigia
Mensajes: 1.471
Antigüedad: 12 años, 7 meses
Puntos: 397
Respuesta: Simple Xml php5

Copié y pegué tu código y me funcionó perfectamente . Lo que hice se llama typecasting para que sea mas fácil acceder a los atributos del objeto xml

Saludos
__________________
Haz preguntas inteligentes-Como ser Hacker
No hacer preguntas por mensaje privado. No sólo no es inteligente sino que es egoísta.
  #11 (permalink)  
Antiguo 15/01/2014, 10:45
 
Fecha de Ingreso: enero-2012
Mensajes: 224
Antigüedad: 12 años, 3 meses
Puntos: 1
Respuesta: Simple Xml php5

genio! me parece que no tengo habilitada la funcion allow_url_fopen por eso no me abre

Etiquetas: php5, simple, xml
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 04:56.