Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP load XML into object

Tags:

oop

php

xml

I am trying to import each story into an object. Each object will have multiple strings (start and end) along with arrays generated from content in the additional-content. An small example would be:

<feed>
  <story>
    <run>
      <start>1/1/2012</start>
      <end>3/1/2012</end>
    </run>
    <additional-content>
      <content>
         <sample>Sample story example</sample>
      </content>
      <content>
         <sample>Sample story example</sample>
      </content>
    </additional-content>
</story>
...
</feed>

I all the xml being imported to a string. Also, I am trying to do this without libraries. I understand looping through each story but am unsure how to load the content into the object while also generating the array correctly. Any help would be appreciated.

like image 860
Matthew Sprankle Avatar asked Jan 09 '12 05:01

Matthew Sprankle


1 Answers

simplexml loads xml into an object and you'll be able to work with it exactly as you're asking.

$xml = simplexml_load_string($stories);
like image 77
Francis Lewis Avatar answered Oct 15 '22 03:10

Francis Lewis