XML ayuda

  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Julio 21st, 2008, 9:51 pm

Alright...realmente no ayudan tanto de una buena partida para mí de aprender.

Lo que quiero hacer es, básicamente, este.

Tiene un documento XML...algo como esto...
Código: [ Select ]
<something>
<title>title</title>
<description>description</description>
<link>link.com</link>
</something>
  1. <something>
  2. <title>title</title>
  3. <description>description</description>
  4. <link>link.com</link>
  5. </something>

y así sucesivamente, lo que quiero hacer es abrir ese archivo en PHP y leer el archivo en consecuencia. Yo podría fácilmente encontrar la manera de hacer eso en ASP como he visto un tutorial de vídeo en algo así, pero me gustaría que en PHP.

Además, ¿cómo puedo usar [url=http://us2.php.net/xml]esto
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Julio 21st, 2008, 9:51 pm

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • Avatar de Usuario
  • Registrado: May 22, 2004
  • Mensajes: 3415
  • Loc: Richland, WA
  • Status: Offline

Nota Julio 21st, 2008, 10:14 pm

Si usted sabe cómo manejar matrices entonces su muy simple

PHP Código: [ Select ]
$xmlData = new SimpleXMLElement('your/xml/file.xml');
//And here's the output.
print_r($xmlData);
  1. $xmlData = new SimpleXMLElement('your/xml/file.xml');
  2. //And here's the output.
  3. print_r($xmlData);
#define NULL (::rand() % 2)
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Julio 21st, 2008, 10:55 pm

Cuando intento que me sale el siguiente error:
Quote:
Advertencia: SimpleXMLElement:: __construct () [function.SimpleXMLElement-- construir]: Entidad: línea 1: parser error: etiqueta de inicio de espera, <no se encuentra en C: \ Wamp \ www \ Nueva carpeta \ xml_test.php en la línea 2

Advertencia: SimpleXMLElement:: __construct () [function.SimpleXMLElement-- construir]: file.xml en C: \ Wamp \ www \ Nueva carpeta \ xml_test.php en la línea 2

Advertencia: SimpleXMLElement:: __construct () [función. SimpleXMLElement --- construir]: ^ en C: \ Wamp \ www \ Nueva carpeta \ xml_test.php en la línea 2

Fatal error: Uncaught excepción "excepción" con String mensaje no puede ser analizada como XML en C: \ Wamp \ www \ Nueva carpeta \ xml_test.php: 2 Stack trace: #0 C: \ Wamp \ www \ Nueva carpeta \ xml_test.php (2): SimpleXMLElement-> __construct (file.xml) #(1) principales arrojados en C: \ Wamp \ www \ Nueva carpeta \ xml_test.php en la línea 2
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • Avatar de Usuario
  • Registrado: May 22, 2004
  • Mensajes: 3415
  • Loc: Richland, WA
  • Status: Offline

Nota Julio 21st, 2008, 11:12 pm

¿Cuál es el código de xml_test.php y el código de su xml doc?
#define NULL (::rand() % 2)
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Julio 21st, 2008, 11:17 pm

file.xml
XML Código: [ Select ]
 <message>
 <to>Bogey</to>
 <from>User</from>
 <heading>This is my heading</heading>
 <body>The body of the message</body>
</message>
  1.  <message>
  2.  <to>Bogey</to>
  3.  <from>User</from>
  4.  <heading>This is my heading</heading>
  5.  <body>The body of the message</body>
  6. </message>

xml_test.php
PHP Código: [ Select ]
<?php
 $xml = new SimpleXMLElement('file.xml');
 //And here's the output.
 print_r($xml);
 //Another attempt.
 echo $xml->asXML();
?>
  1. <?php
  2.  $xml = new SimpleXMLElement('file.xml');
  3.  //And here's the output.
  4.  print_r($xml);
  5.  //Another attempt.
  6.  echo $xml->asXML();
  7. ?>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • righteous_trespasser
  • Scuffle
  • Genius
  • Avatar de Usuario
  • Registrado: Mar 12, 2007
  • Mensajes: 6228
  • Loc: South-Africa
  • Status: Offline

Nota Julio 22nd, 2008, 12:08 am

Bogies ha tenido usted un vistazo a esto ?
Let's leave all our *plum* where it is and go live in the jungle ...
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Julio 22nd, 2008, 12:22 am

righteous_trespasser escribió:
Bogies ha tenido usted un vistazo a esto ?

¿Se ha preguntado por qué mi XML es taaan similar a la que llegamos allí? ;)

Sí tengo, pero sólo fueron allí para obtener el XML...no han pensado en el analizador cosa...php.net pensamiento sería lo suficientemente bueno...

Sin embargo, la explicación existe realmente una mierda...al menos yo lo creo
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • spork
  • Brewmaster
  • Silver Member
  • Avatar de Usuario
  • Registrado: Sep 22, 2003
  • Mensajes: 6134
  • Loc: Seattle, WA
  • Status: Offline

Nota Julio 22nd, 2008, 7:50 am

Bogies, que está recibiendo file.xml errores porque no es un buen cadena XML. Es un nombre de archivo. El constructor para SimpleXMLObject no tiene un nombre de archivo, se necesita XML.

Usted probablemente querrá estar utilizando simplexml_load_file () .
The Beer Monocle. Classy.
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Julio 22nd, 2008, 11:58 am

Muy bien. Gracias spork. , Que ahora funciona, pero ¿cómo puedo ahora convertir desde SimpleXMLElement Object a una matriz?

Lo hice así $ xml = ( matriz ) $ xml ; pero sinces su supone que es una matriz multidimensional, la más interior "conjuntos" son SimpleXMLElement objetos.

He aquí como este "arreglo" se parece.
Código: [ Select ]
Array
(
    [mail] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [to] => Bogey
                    [from] => Runwin2
                    [subject] => Welcome
                    [body] => Welcome to wedevoy.com
                )

            [1] => SimpleXMLElement Object
                (
                    [to] => Bogey
                    [from] => Runwin2
                    [subject] => Welcome
                    [body] => Welcome to wedevoy.com
                )

            [2] => SimpleXMLElement Object
                (
                    [to] => Bogey
                    [from] => Runwin2
                    [subject] => Welcome
                    [body] => Welcome to wedevoy.com
                )

            [3] => SimpleXMLElement Object
                (
                    [to] => Bogey
                    [from] => Runwin2
                    [subject] => Welcome
                    [body] => Welcome to wedevoy.com
                )

        )

)
  1. Array
  2. (
  3.     [mail] => Array
  4.         (
  5.             [0] => SimpleXMLElement Object
  6.                 (
  7.                     [to] => Bogey
  8.                     [from] => Runwin2
  9.                     [subject] => Welcome
  10.                     [body] => Welcome to wedevoy.com
  11.                 )
  12.             [1] => SimpleXMLElement Object
  13.                 (
  14.                     [to] => Bogey
  15.                     [from] => Runwin2
  16.                     [subject] => Welcome
  17.                     [body] => Welcome to wedevoy.com
  18.                 )
  19.             [2] => SimpleXMLElement Object
  20.                 (
  21.                     [to] => Bogey
  22.                     [from] => Runwin2
  23.                     [subject] => Welcome
  24.                     [body] => Welcome to wedevoy.com
  25.                 )
  26.             [3] => SimpleXMLElement Object
  27.                 (
  28.                     [to] => Bogey
  29.                     [from] => Runwin2
  30.                     [subject] => Welcome
  31.                     [body] => Welcome to wedevoy.com
  32.                 )
  33.         )
  34. )

Este XML es para la prueba de lo que podría ser más adelante...aquí es cómo mi archivo XML parece.
Código: [ Select ]
<?xml version="1.0" encoding="ISO-8859-1"?>
<mailer>
<mail>
<to>Bogey</to>
<from>Runwin2</from>
<subject>Welcome</subject>
<body>Welcome to wedevoy.com</body>
</mail>
<mail>
<to>Bogey</to>
<from>Runwin2</from>
<subject>Welcome</subject>
<body>Welcome to wedevoy.com</body>
</mail>
<mail>
<to>Bogey</to>
<from>Runwin2</from>
<subject>Welcome</subject>
<body>Welcome to wedevoy.com</body>
</mail>
<mail>
<to>Bogey</to>
<from>Runwin2</from>
<subject>Welcome</subject>
<body>Welcome to wedevoy.com</body>
</mail>
</mailer>
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <mailer>
  3. <mail>
  4. <to>Bogey</to>
  5. <from>Runwin2</from>
  6. <subject>Welcome</subject>
  7. <body>Welcome to wedevoy.com</body>
  8. </mail>
  9. <mail>
  10. <to>Bogey</to>
  11. <from>Runwin2</from>
  12. <subject>Welcome</subject>
  13. <body>Welcome to wedevoy.com</body>
  14. </mail>
  15. <mail>
  16. <to>Bogey</to>
  17. <from>Runwin2</from>
  18. <subject>Welcome</subject>
  19. <body>Welcome to wedevoy.com</body>
  20. </mail>
  21. <mail>
  22. <to>Bogey</to>
  23. <from>Runwin2</from>
  24. <subject>Welcome</subject>
  25. <body>Welcome to wedevoy.com</body>
  26. </mail>
  27. </mailer>

y si se necesita ayuda, aquí es cómo mi archivo PHP parece.
Código: [ Select ]
<?php
$xml = simplexml_load_file('file.xml');
$xml = (array) $xml;
echo "<pre>";
print_r($xml);
echo "</pre>";
print_r($xml['mail']['0']['to']); // Doesn't print anything
?>
  1. <?php
  2. $xml = simplexml_load_file('file.xml');
  3. $xml = (array) $xml;
  4. echo "<pre>";
  5. print_r($xml);
  6. echo "</pre>";
  7. print_r($xml['mail']['0']['to']); // Doesn't print anything
  8. ?>

Gracias
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Julio 22nd, 2008, 8:42 pm

Alright...Llegué a donde yo quiero...casi. El problema ahora es el error que recibo. Aquí está mi php
PHP Código: [ Select ]
<?php
   $xml = simplexml_load_file('file.xml');
   $xml = (array) $xml;
   //echo "<pre>\n";
   //print_r($xml);
 
   foreach($xml['mail'] as $key => $test)
   {
      echo "<p>\n";
      $test = (array) $test[$key];
      foreach($test as $key => $value)
      {
         echo " <strong>{$key}</strong>: {$value}<br />\n";
      }
      echo "</p>\n";
   }
   //echo "</pre>\n";
?>
  1. <?php
  2.    $xml = simplexml_load_file('file.xml');
  3.    $xml = (array) $xml;
  4.    //echo "<pre>\n";
  5.    //print_r($xml);
  6.  
  7.    foreach($xml['mail'] as $key => $test)
  8.    {
  9.       echo "<p>\n";
  10.       $test = (array) $test[$key];
  11.       foreach($test as $key => $value)
  12.       {
  13.          echo " <strong>{$key}</strong>: {$value}<br />\n";
  14.       }
  15.       echo "</p>\n";
  16.    }
  17.    //echo "</pre>\n";
  18. ?>

y aquí está el error Im getting
Quote:
Warning: main () [function.include]: No se puede agregar el número de correo elemento 3, cuando sólo 0 tales elementos existen en C: \ wamp \ www \ Nueva carpeta \ xml_test.php en la línea 10

Tengo que por cada $ llave de mi pare no foreach.

¿Alguna razón de por qué está haciendo esto?

Mi XML se ve algo como...
XML Código: [ Select ]
<?xml version="1.0" encoding="ISO-8859-1"?>
<mailer>
 <mail>
 <To>Bogey</To>
 <From>Runwin2</From>
 <Subject>Welcome</Subject>
 <Body>Welcome to wedevoy.com</Body>
 </mail>
 <mail>
 <To>Runwin2</To>
 <From>Bogey</From>
 <Subject>RE: Welcome</Subject>
 <Body>Thank you for the welcome</Body>
 </mail>
 <mail>
 <To>Bogey</To>
 <From>Runwin2</From>
 <Subject>RE: RE: Welcome</Subject>
 <Body>Your welcome. This is a friendly community</Body>
 </mail>
 <mail>
 <To>Runwin2</To>
 <From>Bogey</From>
 <Subject>RE: RE: RE: Welcome</Subject>
 <Body>Yeah, I noticed that this is a friendly community</Body>
 </mail>
 <mail>
 <To>Bogey</To>
 <From>Runwin2</From>
 <Subject>RE: RE: RE: RE: Welcome</Subject>
 <Body>Let's us keep it that way ;)</Body>
 </mail>
</mailer>
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <mailer>
  3.  <mail>
  4.  <To>Bogey</To>
  5.  <From>Runwin2</From>
  6.  <Subject>Welcome</Subject>
  7.  <Body>Welcome to wedevoy.com</Body>
  8.  </mail>
  9.  <mail>
  10.  <To>Runwin2</To>
  11.  <From>Bogey</From>
  12.  <Subject>RE: Welcome</Subject>
  13.  <Body>Thank you for the welcome</Body>
  14.  </mail>
  15.  <mail>
  16.  <To>Bogey</To>
  17.  <From>Runwin2</From>
  18.  <Subject>RE: RE: Welcome</Subject>
  19.  <Body>Your welcome. This is a friendly community</Body>
  20.  </mail>
  21.  <mail>
  22.  <To>Runwin2</To>
  23.  <From>Bogey</From>
  24.  <Subject>RE: RE: RE: Welcome</Subject>
  25.  <Body>Yeah, I noticed that this is a friendly community</Body>
  26.  </mail>
  27.  <mail>
  28.  <To>Bogey</To>
  29.  <From>Runwin2</From>
  30.  <Subject>RE: RE: RE: RE: Welcome</Subject>
  31.  <Body>Let's us keep it that way ;)</Body>
  32.  </mail>
  33. </mailer>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • tan_go
  • Banned
  • Banned
  • Avatar de Usuario
  • Registrado: Dic 20, 2006
  • Mensajes: 65
  • Status: Offline

Nota Julio 23rd, 2008, 7:40 am

Difícil de decir forma, sólo ocurrirá de vez en cuando.
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8212
  • Loc: USA
  • Status: Offline

Nota Julio 23rd, 2008, 9:07 am

Lo arreglé. Tuve que reestructurar mi archivo XML que...
Código: [ Select ]
<?xml version="1.0" encoding="ISO-8859-1"?>
<mail>
<To>Bogey</To>
<From>Runwin2</From>
<Subject>Welcome</Subject>
<Body>Welcome to wedevoy.com</Body>
<To>Runwin2</To>
<From>Bogey</From>
<Subject>RE: Welcome</Subject>
<Body>Thank you for the welcome</Body>
<To>Bogey</To>
<From>Runwin2</From>
<Subject>RE: RE: Welcome</Subject>
<Body>Your welcome. This is a friendly community</Body>
<To>Runwin2</To>
<From>Bogey</From>
<Subject>RE: RE: RE: Welcome</Subject>
<Body>Yeah, I noticed that this is a friendly community</Body>
<To>Bogey</To>
<From>Runwin2</From>
<Subject>RE: RE: RE: RE: Welcome</Subject>
<Body>Let's us keep it that way ;)</Body>
</mail>
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <mail>
  3. <To>Bogey</To>
  4. <From>Runwin2</From>
  5. <Subject>Welcome</Subject>
  6. <Body>Welcome to wedevoy.com</Body>
  7. <To>Runwin2</To>
  8. <From>Bogey</From>
  9. <Subject>RE: Welcome</Subject>
  10. <Body>Thank you for the welcome</Body>
  11. <To>Bogey</To>
  12. <From>Runwin2</From>
  13. <Subject>RE: RE: Welcome</Subject>
  14. <Body>Your welcome. This is a friendly community</Body>
  15. <To>Runwin2</To>
  16. <From>Bogey</From>
  17. <Subject>RE: RE: RE: Welcome</Subject>
  18. <Body>Yeah, I noticed that this is a friendly community</Body>
  19. <To>Bogey</To>
  20. <From>Runwin2</From>
  21. <Subject>RE: RE: RE: RE: Welcome</Subject>
  22. <Body>Let's us keep it that way ;)</Body>
  23. </mail>

y cambiar mi php para
PHP Código: [ Select ]
<?php
   $xml = simplexml_load_file('file.xml');
   $xml = (array) $xml;
   //echo "<pre>\n";
   //print_r($xml);
 
   $elements = count($xml);
 
   for($i = 0; $i <= $elements; ++$i)
   {
 
      echo "<p>\n";
      foreach($xml as $key => $value)
      {  
 
         echo " <strong>{$key}</strong>: {$value[$i]}<br />\n";
      }
      echo "</p>\n";
   }
   //echo "</pre>\n";
 
?>
  1. <?php
  2.    $xml = simplexml_load_file('file.xml');
  3.    $xml = (array) $xml;
  4.    //echo "<pre>\n";
  5.    //print_r($xml);
  6.  
  7.    $elements = count($xml);
  8.  
  9.    for($i = 0; $i <= $elements; ++$i)
  10.    {
  11.  
  12.       echo "<p>\n";
  13.       foreach($xml as $key => $value)
  14.       {  
  15.  
  16.          echo " <strong>{$key}</strong>: {$value[$i]}<br />\n";
  17.       }
  18.       echo "</p>\n";
  19.    }
  20.    //echo "</pre>\n";
  21.  
  22. ?>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Publicar Información

  • Total de mensajes en este tema: 12 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 92 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC