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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:31:58+00:00 2026-05-22T18:31:58+00:00

Firstly, may I point out that I am a newcomer to all things PHP

  • 0

Firstly, may I point out that I am a newcomer to all things PHP so apologies if anything here is unclear and I’m afraid the more layman the response the better. I’ve been having real trouble parsing an xml file in to php to then populate an HTML table for my website. At the moment, I have been able to get the full xml feed in to a string which I can then echo and view and all seems well. I then thought I would be able to use simplexml to pick out specific elements and print their content but have been unable to do this.

The xml feed will be constantly changing (structure remaining the same) and is in compressed format. From various sources I’ve identified the following commands to get my feed in to the right format within a string although I am still unable to print specific elements. I’ve tried every combination without any luck and suspect I may be barking up the wrong tree. Could someone please point me in the right direction?!

$file = fopen("compress.zlib://$url", 'r');
$xmlstr = file_get_contents($url);
$xml = new SimpleXMLElement($url,null,true);

foreach($xml as $name) {
  echo "{$name->awCat}\r\n";
}

Many, many thanks in advance,

Chris

PS The actual feed

  • 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-22T18:31:59+00:00Added an answer on May 22, 2026 at 6:31 pm

    Since no one followed my closevote, I think I can just as well put my own comments as an answer:

    First of all, SimpleXml can load URIs directly and it can do so with stream wrappers, so your three calls in the beginning can be shortened to (note that you are not using $file at all)

    $merchantProductFeed = new SimpleXMLElement("compress.zlib://$url", null, TRUE);
    

    To get the values you can either use the implicit SimpleXml API and drill down to the wanted elements (like shown multiple times elsewhere on the site):

    foreach ($merchantProductFeed->merchant->prod as $prod) {
        echo $prod->cat->awCat , PHP_EOL;
    }
    

    or you can use an XPath query to get at the wanted elements directly

    $xml = new SimpleXMLElement("compress.zlib://$url", null, TRUE);
    foreach ($xml->xpath('/merchantProductFeed/merchant/prod/cat/awCat') as $awCat) {
        echo $awCat, PHP_EOL;
    }
    

    Live Demo

    Note that fetching all $awCat elements from the source XML is rather pointless though, because all of them have "Bodycare & Fitness" for value. Of course you can also mix XPath and the implict API and just fetch the prod elements and then drill down to the various children of them.

    Using XPath should be somewhat faster than iterating over the SimpleXmlElement object graph. Though it should be noted that the difference is in an neglectable area (read 0.000x vs 0.000y) for your feed. Still, if you plan to do more XML work, it pays off to familiarize yourself with XPath, because it’s quite powerful. Think of it as SQL for XML.

    For additional examples see

    • A simple program to CRUD node and node values of xml file and
    • PHP Manual – SimpleXml Basic Examples
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, here's my script: #!/usr/bin/python import sys, os sys.path.append('/home/username/python') sys.path.append(/home/username/python/flup) sys.path.append(/home/username/python/django) # more path
Firstly, Modifying may be the wrong term, I see a few people have posted
Firstly, let me set out what I'd like to do. Assume I have three
Firstly, I was wondering if there was some kind of built in function that
Firstly I know that there are many question and solutions to correct thread marshalling
Firstly, I do not have any malicious intent out of this question. I would
Firstly, let me start by saying that I haven't ever directly used SQL views.
Firstly this has turned out to be quite a long post so please bear
After all the answers to my last question about fine-tuning turned out to be
Firstly, sorry about the question title, it may not be very descriptive for my

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.