XML help

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright... not really help as much of a kick-start for me to learn it.

What I want to do is basically this.

Have an XML document... something like this...
Code: [ 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>

and so on, what I want to do is open that file in PHP and read the file accordingly. I could easily figure out how to do that in ASP as I have watched a video tutorial on something like that, but I want it in PHP.

Also, how would I use this to accomplish this? Or is that for something way different?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

If you know how to manage arrays then its fairly simple

PHP Code: [ 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);
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

When I try that I get the following errors:
Quote:
Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 1: parser error : Start tag expected, '<' not found in C:\wamp\www\New Folder\xml_test.php on line 2

Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: file.xml in C:\wamp\www\New Folder\xml_test.php on line 2

Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in C:\wamp\www\New Folder\xml_test.php on line 2

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\wamp\www\New Folder\xml_test.php:2 Stack trace: #0 C:\wamp\www\New Folder\xml_test.php(2): SimpleXMLElement->__construct('file.xml') #1 {main} thrown in C:\wamp\www\New Folder\xml_test.php on line 2
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Whats the code of xml_test.php and the code of your xml doc?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

file.xml
XML Code: [ 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 Code: [ 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. ?>
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

Bogey have you had a look at this?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

righteous_trespasser wrote:
Bogey have you had a look at this?

Have you wondered why my XML is sooo similar to the one they got there? ;)

Yes I have, but only went there to get the XML... haven't thought about the parser thing... thought php.net would be good enough...

But the explanation there really sucks... at least I think so
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

Bogey, you're getting errors because 'file.xml' is not a proper XML string. It's a filename. The constructor for SimpleXMLObject doesn't take a filename, it takes XML.

You probably want to be using simplexml_load_file().
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright. Thanks spork. That now works, but how do I now convert it from SimpleXMLElement Object to an array?

I did that like this $xml = (array) $xml; but sinces it's supposed to be a multidimensional array, the most inner "arrays" are SimpleXMLElement Object.

Here is how that "array" looks like.
Code: [ 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. )

This XML is for the test for what it might be later on... here is how my XML file looks like.
Code: [ 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>

and if it is needed for help, here is how my php file looks like.
Code: [ 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. ?>

Thanks
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright... I got where I want to... almost. The problem now is the error I receive. Here is my php
PHP Code: [ 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. ?>

and here is the error I'm getting
Quote:
Warning: main() [function.main]: Cannot add element mail number 3 when only 0 such elements exist in C:\wamp\www\New Folder\xml_test.php on line 10

I get that for every $key of my parent foreach.

Any reason as to why it is doing this?

My XML looks something like...
XML Code: [ 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>
  • tan_go
  • Banned
  • Banned
  • User avatar
  • Posts: 65

Post 3+ Months Ago

Difficult to say way, just happen from time to time.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I fixed it. I had to restructure my XML file to...
Code: [ 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>

and change my php to
PHP Code: [ 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. ?>

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 63 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.