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

  • Home
  • SEARCH
  • 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 8506939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:43:46+00:00 2026-06-11T02:43:46+00:00

See my PHP: file = routingConfig.xml; global $doc; $doc = new DOMDocument(); $doc->load( $file

  • 0

See my PHP:

file = "routingConfig.xml";
global $doc;
$doc = new DOMDocument();
$doc->load( $file );

function traverseXML($ElTag, $attr = null, $arrayNum = 'all'){
    $tag = $doc->getElementsByTagName($ElTag);
    $arr = array();

    foreach($tag as $el){
        $arr[] = $el->getAttribute($attr);
    }
    if ($arrayNum == 'all'){
        return $arr;
    }else if(is_int($arrayNum)){
        return $arr[$arrayNum];
    }else{
        return "Invalid $arrayNum value: ". $arrayNum;
    }; 
}

echo traverseXML("Route", "type", 2);

XML is:

<Routes>
  <Route type="source"></Route>
  <Route></Route>
<Routes>

Error returned is:

Fatal error: Call to a member function getElementsByTagName() on a non-object

I’m not sure how to do this?

EDIT: Here is the actual code being used. I originally stripped it a little bit trying to make it easier to read, but I think my problem is related to using the function.

  • 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-11T02:43:47+00:00Added an answer on June 11, 2026 at 2:43 am

    Your problem is that the global $doc; statement is outside the function, so the variable $doc is not defined inside the function.

    This would fix it:

    // ...
    function traverseXML($ElTag, $attr = null, $arrayNum = 'all') {
      global $doc;
    // ...
    

    …but

    Global variables are bad news. They usually indicate poor design.

    Really you should pass $doc in as an argument, like this:

    function traverseXML($doc, $ElTag, $attr = null, $arrayNum = 'all'){
        $tag = $doc->getElementsByTagName($ElTag);
        $arr = array();
        foreach($tag as $el){
            $arr[] = $el->getAttribute($attr);
        }
        if ($arrayNum == 'all'){
            return $arr;
        }else if(is_int($arrayNum)){
            return $arr[$arrayNum];
        }else{
            return "Invalid $arrayNum value: ". $arrayNum;
        }; 
    
    }
    
    $file = "routingConfig.xml";
    
    $doc = new DOMDocument();
    $doc->load( $file );
    
    echo traverseXML($doc, "Route", "type", 2);
    

    Although you might consider whether you need the function at all – if you don’t use it anywhere else in you application, you might as well just do this:

    $file = "routingConfig.xml";
    
    $ElTag = "Route";
    $attr = "type";
    $arrayNum = 2;
    
    $doc = new DOMDocument();
    $doc->load( $file );
    
    $tag = $doc->getElementsByTagName($ElTag);
    $arr = array();
    foreach($tag as $el){
        $arr[] = $el->getAttribute($attr);
    }
    if ($arrayNum == 'all'){
        echo $arr;
    }else if(is_int($arrayNum)){
        echo $arr[$arrayNum];
    }else{
        echo "Invalid $arrayNum value: ". $arrayNum;
    }; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP has a is_readable function which checks to see if the file is readable
When I require any PHP file in my index page I see an error:
In wordpress, I have the loop.php file. There I can see the 'read more'
using Fast CGI I can't get it to read the php.ini file. See my
I am running WAMP server. On file upload using PHP I see $_FILES[tmp_name] =>
See the following code of the <?php class DefaultController extends Controller { public function
I am developing a PHP website, but I can’t see the php.ini file on
I have a PHP file which inserts $mail and $password in my database. See
There is a php file ( current.php ) with some variables, like: function do()
I have a PHP file which produces an Xml sitemap based on data which

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.