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 7608481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:55:26+00:00 2026-05-31T00:55:26+00:00

ok this is driving me crazy. I have been trying to parse a xml

  • 0

ok this is driving me crazy.
I have been trying to parse a xml file into a specific array or object so I can compare it to a similar file to test for differences.
However I have had no luck. I have been attempting to use SimpleXMLIterator and SimpleXMLElement to do this.
Here are some samples:

<xml>
 //This is the first record of 1073
    <viddb>
        <movies>1074</movies>
        <movie>
            <title>10.5</title>
            <origtitle>10.5</origtitle>
            <year>2004</year>
            <genre>Disaster</genre>
            <release></release>
            <mpaa></mpaa>
            <director>John Lafia</director>
            <producers>Howard Braunstein, Jeffrey Herd</producers>
            <actors>Kim Delaney, Fred Ward, Ivan Sergei</actors>
            <description>An earthquake reaching a 10.5 magnitude on the Richter scale, strikes the west coast of the U.S. and Canada. A large portion of land falls into the ocean, and the situation is worsened by aftershocks and tsunami.</description>
            <path>E:\www\Media\Videos\Disaster\10.5.mp4</path>
            <length>164</length>
            <size>3648</size>
            <resolution>640x272</resolution>
            <framerate>29.97</framerate>
            <videocodec>AVC</videocodec>
            <videobitrate>2966</videobitrate>
            <label>Roku Media</label>
            <poster>images/10.5.jpg</poster>
        </movie>

Here is the object this record produces using $iter = new SimpleXMLIterator($xml, 0, TRUE);

object(SimpleXMLIterator)#71 (1) {
  ["viddb"] => object(SimpleXMLIterator)#72 (2) {
    ["movies"] => string(4) "1074"
    ["movie"] => array(1074) {
      [0] => object(SimpleXMLIterator)#73 (19) {
        ["title"] => string(4) "10.5"
        ["origtitle"] => string(4) "10.5"
        ["year"] => string(4) "2004"
        ["genre"] => string(8) "Disaster"
        ["release"] => object(SimpleXMLIterator)#1158 (0) {
        }
        ["mpaa"] => object(SimpleXMLIterator)#1159 (0) {
        }
        ["director"] => string(10) "John Lafia"
        ["producers"] => string(31) "Howard Braunstein, Jeffrey Herd"
        ["actors"] => string(35) "Kim Delaney, Fred Ward, Ivan Sergei"
        ["description"] => string(212) "An earthquake reaching a 10.5 magnitude on the Richter scale, strikes the west coast of the U.S. and Canada. A large portion of land falls into the ocean, and the situation is worsened by aftershocks and tsunami."
        ["path"] => string(37) "E:\www\Media\Videos\Disaster\10.5.mp4"
        ["length"] => string(3) "164"
        ["size"] => string(4) "3648"
        ["resolution"] => string(7) "640x272"
        ["framerate"] => string(5) "29.97"
        ["videocodec"] => string(3) "AVC"
        ["videobitrate"] => string(4) "2966"
        ["label"] => string(10) "Roku Media"
        ["poster"] => string(15) "images/10.5.jpg"
      }

What I’m trying to produce (at the moment) is a single level associative array for each movie . All the examples I’ve read on and followed always produced an array of arrays, which is much more difficult to work with.

This is were i’m at :

$iter = new SimpleXMLIterator($xml, 0, TRUE);
        Zend_Debug::dump($iter);
        //so far xpath has not worked for me, I can't get $result to return anything
        $result = $iter->xpath('/xml/viddb/movies/movie');
        $movies = array();
        for ($iter->rewind(); $iter->valid(); $iter->next()) {
            foreach ($iter->getChildren() as $key => $value) {
                //I can get each movie title to echo but when I try to put them into an
                // array it only has the last record
                echo $value->title . '<br />';
                $movies['title'] = $value->title;

            }
        }
        return $movies;

I feel like I’m missing something simple and obvious…as usual 🙂
[EDIT]
I found my error, I was tripping over the array of objects thing. I had to cast the data I wanted as a string to make it work how I wanted. Just for info here is what I came up with to put me on the track I wanted:

public function indexAction() {
        $xml = APPLICATION_PATH . '/../data/Videos.xml';
        $iter = new SimpleXMLElement($xml, 0, TRUE);
        $result = $iter->xpath('//movie');

        $movies = array();
        foreach ($result as $key => $movie) {
            $movies[$key + 1] = (string) $movie->title;
        }
        Zend_Debug::dump($movies, 'Movies');
    }
  • 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-31T00:55:28+00:00Added an answer on May 31, 2026 at 12:55 am

    XPATH is the answer you are looking for. I think the reason your XPATH isn’t working is because you are looking for a movie node under the movies node when the movies node does not have any children.

    Edit: Think it might be easier to just use a foreach loop instead of the iterator. I had to look up the iterator as I had never seen it before. Been using simplxml and xpath for a while too. Also, I believe you should only use SimpleXMLElement if you are planning on editing the XML as well. If you simply want to read it for comparison, best to use simplexml_load_file. You can also change your xpath to simply.

    xpath('//movie');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been driving me crazy for 2 days. I have been trying to
This has been driving me crazy for the past few minutes I have a
This is driving me crazy. I have this one php file on a test
This has been driving me crazy for the past couple of hours. I have
This has been driving me crazy.. I am trying to return a value from
This is driving me crazy, I just can't find out the problem: I have
I've been driving myself crazy trying to make this work. Variations of my attempt
This is driving me crazy. In the past I have been able to have
This is driving me crazy. I have a very simple user control: public int?
Ok, this is driving me crazy. I have a CA that needs to know

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.