SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Parsing XML with SimpleXML

    Looking for some help parsing an XML document with SimpleXML. First, a few points of reference:

    Original XML file: http://pastebin.com/Dn1h62bu
    Loaded with SimpleXML: http://pastebin.com/BZV6wAMZ

    The second pasetbin page shows where I'm at. I've got the data into an array, albeit a confusing one. This array has two "sets" of data, one named Carter Club and the other Frontier Vineyards. My goal is to get the values for the [diningAttributes] array out, for Carter Club.

    I am stuck with the syntax of how to get that specific data out of this array. Obviously the output of what I'm looking for is an array, but just a subset of this larger one.

    It's also worth noting that while the Carter Club information resides under index 0, I won't know that. If I wanted to access the information under Frontier Vineyards instead, I would only have the site name and not have the index value.

    Thoughts?
    TAKE A WALK OUTSIDE YOUR MIND.

  2. #2
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I decided to go a different route and used xml_parse_create() and xml_parse_into_struct() to create a ginormous number of arrays. After a few ugly nested loops, I managed to get the data I was after. No need to hurt your head over this one.
    TAKE A WALK OUTSIDE YOUR MIND.

  3. #3
    SitePoint Enthusiast Adam Chrapkowski's Avatar
    Join Date
    Sep 2011
    Location
    Poland
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Suppose your object is called $obj, then:

    PHP Code:
    function myFunc($obj) {
      foreach(
    $obj->siteProfile as $profile) {
        if(
    $profile->siteName == "Carter Club") {
          return 
    $profile->diningSiteProfile->diningTableAttribute;
        }
      }


  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    55 Post(s)
    Tagged
    0 Thread(s)
    Building on Adam's example, you could use XPath to find the site profile rather than loop over all of them.

    PHP Code:
    $profiles $obj->xpath('siteProfile[siteName = "Carter Club"]');
    if (!empty(
    $profiles)) {
        
    $profile $profiles[0];
        
        
    // Do something with the proflie
        
    echo $profile->siteAddress->addres1;

    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Thanks to both of you!
    TAKE A WALK OUTSIDE YOUR MIND.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •