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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:19:23+00:00 2026-06-18T05:19:23+00:00

I am experiencing a very odd error. I am writing a function to validate

  • 0

I am experiencing a very odd error. I am writing a function to validate XML based on element names, in this case passed in as an array. First, I normalize the input to a SimpleXMLElement object; in this case I am passing in a DOMDocument, and I tested it, it is converting correctly to SimpleXMLElement.

What confuses me is that calling getName() will throw the following:

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

But using exit($xml->getName()) (or echo) will return the correct result – the root element name!

Relevant code:

function validateXML($xml, $format='') {
(get_class($xml) !== 'SimpleXMLElement') ?
    ((get_class($xml) === 'DOMDocument') ?
        $xml = simplexml_import_dom($xml)
        : $xml = simplexml_load_string($xml))
    : $xml = $xml;
$rootName = ($xml->getName());
if ($rootName != $format[0]) {
    exit($xml->getName())));
}

Also, in case it matters, the server is running PHP 5.3

I am very new to PHP so help is extremely appreciated!

P.S. To be clear about what exactly is going on, the following code:

function validateXML($xml, $format='') {
    echo(get_class($xml);
    $xml = simplexml_import_dom($xml);
    echo(get_class($xml);
    echo($xml->getName());
}

outputs to the server

DOMDocument
SimpleXMLElement
'ElementName' (the correct root name of my XML document)

… so it appears to be working, except that the fatal error still gets thrown.

  • 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-18T05:19:24+00:00Added an answer on June 18, 2026 at 5:19 am

    Your code (assuming it is close enough to what you have written in the question) will fail when $xml is invalid XML, because simplexml_load_string():

    Returns an object of class SimpleXMLElement with properties containing the data held within the xml document, or FALSE on failure.

    If you still do not see this, I suggest rewriting your code using if statements instead of tertiary statements. Your function, when $xml is not SimpleXMLElement instance, nor DOMDocument instance, will be effectively coming down to the following function:

    function validateXML($xml, $format='') {
        simplexml_load_string($xml);
        $rootName = ($xml->getName());
        if ($rootName != $format[0]) {
            exit($xml->getName());
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am experiencing some very odd timing behavior from a function I wrote. If
I've been experiencing this very odd problem lately in Heroku. I develop and test
I am experiencing very odd performance on DB2 version 9.1 when running the query
I'm experiencing this error... variable 'paymentLine' of type 'Xrm.sb_directdebitpaymentline' referenced from scope '', but
I'm experiencing a very odd problem with a Java application running under Tomcat. We
I'm experiencing a very strange (and annoying) linker error on my project. Say for
we are experiencing some very odd errors in our installation. Some times out of
I am experiencing a very strange error (could be a bug). I am trying
I'm experiencing a very odd upload problem. Here's the relevant view file: <form action=http://localhost/index.php/temp/upload/
I am experiencing a very odd bug with Chrome 17.0.963.56. Following is a screenshot:

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.