Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:44:55+00:00 2026-05-30T09:44:55+00:00

I am trying to parse an xml document in php which is somewhat tough

  • 0

I am trying to parse an xml document in php which is somewhat tough because the xml consists of objects within objects. Here is the xml:

<Bars>
<Bar>
<BarName>the bar name</BarName>
<bar_id>0</bar_id>
<Bartenders>
<Bartender><fname>a first name</fname><lname>a last name</lname><imageURL>an image url</imageURL><shift>2</shift></Bartender>
</Bartenders>
<Events>
<Event><EventName> event</EventName><date>08/10/1989</date></Event>
</Events>
<Specials>
<Special> Special 1</Special><Special> Special 2</Special>
</Specials>
</Bar>

Obviously there are several of these ‘bar’ objects within the bars xml element. What I am trying to figure is out how to do is parse this file, save the contents to an array, update the array with user input from an iOS app, and finally write the XML back to the page. The problem I am having is, how will I be able to save several ‘bartender’, ‘event’ and ‘special’ objects into the arrays for the respective bar? I will have to create a bartender array in the foreach array correct? Do I need to create a bar class which houses bartender, event, and special arrays?

Here is a piece of my php code thus far:

$bars = $doc->getElementsByTagName("Bar");
foreach($bars as $bar)
{
    $barNames = $bar->getElementsByTagName("BarName");
    $barName = $barNames->item(0)->nodeValue;
    array_push($barNameArray, $barName);


    echo "<b>barName:$barName</b><br>";
}

$barsArray = array();

//make any updates to the arrays as needed

for($i = 0; $i < count($barNameArray); $i++) // write an array of bar objects from the arrays created above

As you can see, I have it figured out quite easily if it’s just one type of element per bar. I can’t hardcode anything in because I won’t know how many bartenders, bars, specials, etc are posted at a given time… Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:44:56+00:00Added an answer on May 30, 2026 at 9:44 am

    Try this function to get associative array of your XML.

    function objectsIntoArray($arrObjData, $arrSkipIndices = array())
    {
        $arrData = array();
    
        // if input is object, convert into array
        if (is_object($arrObjData)) {
            $arrObjData = get_object_vars($arrObjData);
        }
    
        if (is_array($arrObjData)) {
            foreach ($arrObjData as $index => $value) {
                if (is_object($value) || is_array($value)) {
                    $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
                }
                if (in_array($index, $arrSkipIndices)) {
                    continue;
                }
                $arrData[$index] = $value;
            }
        }
        return $arrData;
    }
    

    Usage:

    $xmlUrl = "nameit.xml"; // XML 
    $xmlStr = file_get_contents($xmlUrl);
    $xmlObj = simplexml_load_string($xmlStr);
    $arrXml = objectsIntoArray($xmlObj);
    print_r($arrXml);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse an xml document I created in a php file
I've been trying to write a PHP script to parse an XML document using
I'm trying to parse an XML document that has an attribute called link which
So I'm trying to parse the following XML document with C#, using System.XML: <root
I'm trying to parse and load an XML document, however I'm getting this exception
I'm trying to use XPath to parse an XML document. One of my NSXMLElement's
Trying to do a simple parse of an XML document. What's the easiest way
So I'm trying to parse some XML, the creation of which is not under
I am beginner in PHP. I am trying to parse this xml file. <relationship>
I am trying to parse an XMl document that i received into a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.