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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:24:11+00:00 2026-06-13T10:24:11+00:00

I seem to be getting back a well-formed XML document from this cURL, but

  • 0

I seem to be getting back a well-formed XML document from this cURL, but I can’t seem to parse this return using the SimpleXMLElement class. This is the iteration of code that I’m working with now:

$url = "http://rosemary.umw.edu/~stephen/cpsc448/numCredits.php?prefix=". $prefix ."&number=" . $number; // prefix=cpsc&number=220
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 15);

$resp = curl_exec($ch);

echo $resp;

This returns:

<?xml version="1.0"?><NumCredits>4</NumCredits>

This is how I’m currently trying to parse the return:

$xml = new SimpleXMLElement($resp);
echo $xml->NumCredits[0];

Using this I don’t get a response. I’ve also tried this method without success:

$dom = new DOMDocument;
$dom->loadXML($resp);
if (!$dom) {
    echo 'Error parsing document';
    exit;   
}

$simpleXML = simplexml_import_dom($dom);
echo $simpleXML;
return $simpleXML->NumCredits[0];

I have to be doing something wrong here but I can’t quite figure out why it’s so hard to parse this return.

  • 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-06-13T10:24:12+00:00Added an answer on June 13, 2026 at 10:24 am

    You only have one element in your XML, so it is considered the document root:

    $xml_string = '<?xml version="1.0"?><NumCredits>4</NumCredits>';
    $xml = new SimpleXmlElement($xml_string);
    print_r($xml);
    

    Will output:

    SimpleXMLElement Object
    (
        [0] => 4
    )
    

    Thus it can be accessed as $xml[0];

    If you want to be able to access it by the element name, you will need to add a root element to your XML, for example:

    <?xml version="1.0"?>
    <root>
        <NumCredits>4</NumCredits>
    </root>
    

    Once this document is parsed, you can access the value using $xml->NumCredits;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been coding for a while now but seem to be getting this issue
This may seem like a simple question but i am getting an error when
This is a string I am getting back from FB { id: 5672*****, name:
I seem to be getting this particular always when i am trying to receive
Spent a good few hours on this now and don't seem to be getting
I've added # config/initializers/will_paginate_array_fix.rb require 'will_paginate/array' but still do not seem to be getting
I'm getting the following error message and I can't seem to figure out the
I seem to go back and forth on this, sometimes it works for me,
I'm just now getting into instantiating JavaScript object literals using a prototype constructor, but
Similar to what I asked in this question a while back about getting the

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.