Foros del Web » Programando para Internet » PHP »

Consulta de archivo XML

Estas en el tema de Consulta de archivo XML en el foro de PHP en Foros del Web. Hola buenas, Estoy leyendo un archivo xml y no me devuelve valores: Archivo XML: Código PHP: <!--  source :  xxxxxxx --> < ICE -interface  xsi ...
  #1 (permalink)  
Antiguo 22/03/2012, 04:09
Avatar de syntex  
Fecha de Ingreso: noviembre-2002
Ubicación: Cataluña
Mensajes: 978
Antigüedad: 21 años, 5 meses
Puntos: 4
Consulta de archivo XML

Hola buenas,

Estoy leyendo un archivo xml y no me devuelve valores:

Archivo XML:
Código PHP:
<!-- sourcexxxxxxx-->
<
ICE-interface xsi:noNamespaceSchemaLocation="http://xxxxxxxxxx/xsd/files.index.xsd">
  <
files.index Generated="2012335566">
       <
file path="/exports//ESP/468893.xml" Product_ID="468893" Updated="20120307013032" Quality="ICE" Supplier_id="1" Prod_ID="CB568331EE" Catid="377" On_Market="1" Model_Name=" CB568331EE " Product_View="7934094" HighPic="http://images.xxxxxxxx.xxx/img/norm/high/468893-HP.jpg" HighPicSize="35979" HighPicWidth="400" HighPicHeight="400">
       </
file>
  </
files.index>
</
ICE-interface> 
Y no puedo leer la imagen tag file -> HighPic

Código PHP:
# FICHERO DEL PROVEEDOR
# ************************************************************************
$ruta_PartNumber "http://xxx:xxx@xxx/xml_server3.cgi?prod_id=".trim(strtolower($PartNumber)).";vendor=".trim(strtolower($Marca)).";lang=ES;output=metaxml";
                                
# BUSCAR IMG DEL XML DEL PROVEEDOR
# ************************************************************************                
if ($xml_PartNumber simplexml_load_file($ruta_PartNumber))
{
                    
          foreach (
$xml_PartNumber as $key => $xml_img)
          {                    
                echo 
"File: ".utf8_decode(trim($xml_img->file))."<br>";
          }
                    
          return;
                    

Muchas gracias.
__________________
------------------------------------------------
La paciencia es el rey de la ciencia
------------------------------------------------

  #2 (permalink)  
Antiguo 22/03/2012, 12:14
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Consulta de archivo XML

Si después de leer el archivo haces un var_dump($xml_PartNumber); y aparece algo como:

boolean => false

Entonces no se encontró el archivo, ya sea porque la URL no es correcta o no está codificada correctamente; suponiendo que:

$PartNumber = 'abc123', y $Marca = 'marca con espacios'

Entonces:

$ruta_PartNumber = "http://xxx:xxx@xxx/xml_server3.cgi?prod_id=abc123;vendor=marca con espacios;lang=ES;output=metaxml";

El separador de parámetros debe ser &, a menos que hayas configurado tu servidor (o tengas una función en el script) para que sea ;

Ahora, hay algunos caracteres especiales, como espacios, tildes, el mismo &, etc. que deben codificarse para URL:

$ruta_PartNumber = "http://xxx:xxx@xxx/xml_server3.cgi?prod_id=" . urlencode(trim(strtolower($PartNumber))) . ";vendor=" . urlencode(trim(strtolower($Marca))) . ";lang=ES;output=metaxml";
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 22/03/2012, 12:59
Avatar de syntex  
Fecha de Ingreso: noviembre-2002
Ubicación: Cataluña
Mensajes: 978
Antigüedad: 21 años, 5 meses
Puntos: 4
Respuesta: Consulta de archivo XML

El archivo XML me lo da correcto y me da un monton de código array y lo visualizo perfectamente ya que el foreach me detecta el array del archivo XML.
Pero no puedo leer el tag "file -> HighPic"

Eso si añadire urlencode( en mis variables).
__________________
------------------------------------------------
La paciencia es el rey de la ciencia
------------------------------------------------

  #4 (permalink)  
Antiguo 22/03/2012, 13:22
Avatar de andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 9 meses
Puntos: 793
Respuesta: Consulta de archivo XML

Ya intentaste así?

Código PHP:
Ver original
  1. <?php
  2.  
  3. $xml = '<ICE-interface xsi:noNamespaceSchemaLocation="http://xxxxxxxxxx/xsd/files.index.xsd">
  4.  <files.index Generated="2012335566">
  5.       <file path="/exports//ESP/468893.xml" Product_ID="468893" Updated="20120307013032" Quality="ICE" Supplier_id="1" Prod_ID="CB568331EE" Catid="377" On_Market="1" Model_Name=" CB568331EE " Product_View="7934094" HighPic="http://images.xxxxxxxx.xxx/img/norm/high/468893-HP.jpg" HighPicSize="35979" HighPicWidth="400" HighPicHeight="400">
  6.       </file>
  7.  </files.index>
  8. </ICE-interface>';
  9.  
  10. $sxe = new SimpleXMLElement($xml);
  11. echo $sxe->{'files.index'}->file['HighPic'];
  12. //http://images.xxxxxxxx.xxx/img/norm/high/468893-HP.jpg

o algo así?

Código PHP:
Ver original
  1. <?php
  2.  
  3. $xml = '<ICE-interface xsi:noNamespaceSchemaLocation="http://xxxxxxxxxx/xsd/files.index.xsd">
  4.  <files.index Generated="2012335566">
  5.       <file path="/exports//ESP/468893.xml" Product_ID="468893" Updated="20120307013032" Quality="ICE" Supplier_id="1" Prod_ID="CB568331EE" Catid="377" On_Market="1" Model_Name=" CB568331EE " Product_View="7934094" HighPic="http://images.xxxxxxxx.xxx/img/norm/high/468893-HP.jpg" HighPicSize="35979" HighPicWidth="400" HighPicHeight="400">
  6.       </file>
  7.  </files.index>
  8. </ICE-interface>';
  9.  
  10. $sxe = new SimpleXMLElement($xml);
  11.  
  12. foreach ($sxe->{'files.index'}->file->attributes() as $c => $v) {
  13.     echo "$c => $v <br />";
  14. }

Cita:
path => /exports//ESP/468893.xml
Product_ID => 468893
Updated => 20120307013032
Quality => ICE
Supplier_id => 1
Prod_ID => CB568331EE
Catid => 377
On_Market => 1
Model_Name => CB568331EE
Product_View => 7934094
HighPic => http://images.xxxxxxxx.xxx/img/norm/high/468893-HP.jpg
HighPicSize => 35979
HighPicWidth => 400
HighPicHeight => 400
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP
  #5 (permalink)  
Antiguo 22/03/2012, 13:47
Avatar de syntex  
Fecha de Ingreso: noviembre-2002
Ubicación: Cataluña
Mensajes: 978
Antigüedad: 21 años, 5 meses
Puntos: 4
Respuesta: Consulta de archivo XML

Introduciendo este script:

$ruta_PartNumber = "http://xxx:[email protected]/xml_s3/xml_server3.cgi?prod_id=".trim(strtolower($PartNum ber)).";vendor=".trim(strtolower($Marca)).";lang=E S;output=metaxml";
$sxe = new SimpleXMLElement($xml);

foreach ($sxe->{'files.index'}->file->attributes() as $c => $v) {
echo "$c => $v <br />";
}

Me tira este error:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found in xxxxxx
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: http://
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in C:
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:

Con "simplexml_load_file($ruta_PartNumber)", no se podria leer el atributo?
__________________
------------------------------------------------
La paciencia es el rey de la ciencia
------------------------------------------------

  #6 (permalink)  
Antiguo 22/03/2012, 13:50
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Consulta de archivo XML

Es que no tienes definida la variable $xml

Que obtuviste con var_dump($xml_PartNumber);?
__________________
- León, Guanajuato
- GV-Foto
  #7 (permalink)  
Antiguo 22/03/2012, 14:01
Avatar de syntex  
Fecha de Ingreso: noviembre-2002
Ubicación: Cataluña
Mensajes: 978
Antigüedad: 21 años, 5 meses
Puntos: 4
Respuesta: Consulta de archivo XML

Tengo este script y me tira el mismo error:

$ruta_PartNumber = "http://xxx:[email protected]/xml_s3/xml_server3.cgi?prod_id=".trim(strtolower($PartNum ber)).";vendor=".trim(strtolower($Marca)).";lang=E S;output=metaxml";
var_dump($ruta_PartNumber);
# BUSCAR IMG DEL XML DEL PROVEEDOR
# ************************************************** *********************
$sxe = simplexml_load_string($ruta_PartNumber);
foreach ($sxe->{'files.index'}->file->attributes() as $c => $v)
{
echo "$c => $v <br />";
}
__________________
------------------------------------------------
La paciencia es el rey de la ciencia
------------------------------------------------

  #8 (permalink)  
Antiguo 22/03/2012, 14:08
Avatar de syntex  
Fecha de Ingreso: noviembre-2002
Ubicación: Cataluña
Mensajes: 978
Antigüedad: 21 años, 5 meses
Puntos: 4
Respuesta: Consulta de archivo XML

El var_dump me tira:

string(134) "http://xxxx:[email protected]/xml_s3/xml_server3.cgi?prod_id=xxxxx;vendor=xxxxxxxx;lang =ES;output=metaxml"
__________________
------------------------------------------------
La paciencia es el rey de la ciencia
------------------------------------------------

  #9 (permalink)  
Antiguo 23/03/2012, 02:42
Avatar de syntex  
Fecha de Ingreso: noviembre-2002
Ubicación: Cataluña
Mensajes: 978
Antigüedad: 21 años, 5 meses
Puntos: 4
Respuesta: Consulta de archivo XML

Solo e podido consultarlo así:

Código PHP:
if ($sxe = new SimpleXMLElement(file_get_contents $ruta_PartNumber ), NULLfalse))
                {
                    echo 
$ruta_PartNumber."<br>";
                    if (
$sxe->{'files.index'}->file['HighPic'])
                    {
                        echo 
"-".$sxe->{'files.index'}->file['HighPic']."<br>";
                        echo 
"-".$sxe->{'files.index'}->file['HighPicSize']."<br>";
                        echo 
"-".$sxe->{'files.index'}->file['HighPicWidth']."<br>";
                        echo 
"-".$sxe->{'files.index'}->file['HighPicHeight']."<br>";
                        echo 
"*************************************<br>";
                      }
                    
                    return;
                    
                } 
Como podria hacerlo con foreach? Por si tuviese 2 objetos de xml {files.index->file} o más imagenes?
__________________
------------------------------------------------
La paciencia es el rey de la ciencia
------------------------------------------------


Etiquetas: 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 05:00.