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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:36:55+00:00 2026-06-13T20:36:55+00:00

Simple XMLElement Object ( [IpStatus] => 1 [ti_pid_20642] => SimpleXmlElement Object ( I have

  • 0
Simple XMLElement Object
(    
         [IpStatus] => 1    
         [ti_pid_20642] => SimpleXmlElement Object    
               (

I have a SimpleXMLElment in above format and this XML is generated at run time and it’s node values like ti_pid_20642 are partly dnymaic, for example ti_pid_3232, ti-pid_2323, ti_pid_anyumber.

My question is how can I get these nodes values and it’s children using PHP?

  • 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-13T20:36:57+00:00Added an answer on June 13, 2026 at 8:36 pm

    To get all node names that are used in an XML string with SimpleXML you can use the SimpleXMLIterator:

    $tagnames = array_keys(iterator_to_array(
        new RecursiveIteratorIterator(
            new SimpleXMLIterator($string)
            , RecursiveIteratorIterator::SELF_FIRST
        )
    ));
    
    print_r($tagnames);
    

    Which could give you exemplary (you did not give any XML in your question, Demo):

    Array
    (
        [0] => IpStatus
        [1] => ti_pid_20642
        [2] => dependend
        [3] => ti-pid_2323
        [4] => ti_pid_anyumber
        [5] => more
    )
    

    If you have problems to provide a string that contains valid XML, take your existing SimpleXMLelement and create an XML string out of it:

    $string = $simpleXML->asXML();
    

    However, if you like to get all tagnames from a SimpleXML object but you don’t want to convert it to a string, you can create a recursive iterator for SimpleXMLElement as well:

    class SimpleXMLElementIterator extends IteratorIterator implements RecursiveIterator
    {
        private $element;
    
        public function __construct(SimpleXMLElement $element) {
            parent::__construct($element);
        }
    
        public function hasChildren() {
            return (bool)$this->current()->children();
        }
    
        public function getChildren() {
            return new self($this->current()->children());
        }
    }
    

    The usage of it would be similar (Demo):

    $it      = new RecursiveIteratorIterator(
        new SimpleXMLElementIterator($xml), RecursiveIteratorIterator::SELF_FIRST
    );
    $tagnames = array_keys(iterator_to_array($it));
    

    It just depends on what you need.

    This becomes less straight forward, with namespaced elements. Depending if you want to get the local names only or the namspace names or even the namespace URIs with the tagnames.

    The given SimpleXMLElementIterator could be changed to support the iteration over elements across namespaces, by default simplexml only offers traversal over elements in the default namespace:

    /**
     * SimpleXMLElementIterator over all child elements across namespaces 
     */
    class SimpleXMLElementIterator extends IteratorIterator implements RecursiveIterator
    {
        private $element;
    
        public function __construct(SimpleXMLElement $element) {
            parent::__construct(new ArrayIterator($element->xpath('./*')));
        }
    
        public function key() {
            return $this->current()->getName();
        }
    
        public function hasChildren() {
            return (bool)$this->current()->xpath('./*');
        }
    
        public function getChildren() {
            return new self($this->current());
        }
    }
    

    You would then need to check for the namespace per each element- As an example a modified XML document making use of namespaces:

    <root xmlns="namspace:default" xmlns:ns1="namespace.numbered.1">
        <ns1:IpStatus>1</ns1:IpStatus>
        <ti_pid_20642>
            <dependend xmlns="namspace:depending">
                <ti-pid_2323>ti-pid_2323</ti-pid_2323>
                <ti_pid_anyumber>ti_pid_anyumber</ti_pid_anyumber>
                <more xmlns:ns2="namspace.numbered.2">
                    <ti_pid_20642 ns2:attribute="test">ti_pid_20642</ti_pid_20642>
                    <ns2:ti_pid_20642>ti_pid_20642</ns2:ti_pid_20642>
                </more>
            </dependend>
        </ti_pid_20642>
    </root>
    

    Combined with the update SimpleXMLIterator above the following example-code demonstrates the new behavior:

    $xml = new SimpleXMLElement($string);
    $it  = new RecursiveIteratorIterator(
        new SimpleXMLElementIterator($xml), RecursiveIteratorIterator::SELF_FIRST
    );
    
    $count = 0;
    foreach ($it as $name => $element) {
        $nsList = $element->getNamespaces();
        list($ns, $nsUri) = each($nsList);
        printf("#%d:  %' -20s  %' -4s  %s\n", ++$count, $name, $ns, $nsUri);
    }
    

    Output (Demo):

    #1:  IpStatus              ns1   namespace.numbered.1
    #2:  ti_pid_20642                namspace:default
    #3:  dependend                   namspace:depending
    #4:  ti-pid_2323                 namspace:depending
    #5:  ti_pid_anyumber             namspace:depending
    #6:  more                        namspace:depending
    #7:  ti_pid_20642                namspace:depending
    #8:  ti_pid_20642          ns2   namspace.numbered.2
    

    Have fun.

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

Sidebar

Related Questions

I have this SimpleXMLElement object with a XML setup similar to the following... $xml
I have the following XML array: [link]=> array(2) { [0]=> object(SimpleXMLElement)#311 (1) { [@attributes]=>
I have a web service that returns a simple object: [System.CodeDom.Compiler.GeneratedCodeAttribute(System.Xml, 2.0.50727.4927)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()]
I have a SimpleXMLElement like this: SimpleXMLElement Object ( [trailer] => SimpleXMLElement Object (
I have a simple XElement object XElement xml = new XElement(XML, new XElement (TOKEN,Session[Token]),
How to loop through a SimpleXMLElement object? This is what I have: I tried
I have an XML stream parsed to a SimpleXMLElement Object and I am trying
Have this print output from print_r($theobject); SimpleXMLElement Object ( [@attributes] => Array ( [label]
I have this object parsed using SimpleXML: SimpleXMLElement Object ( [contact] => SimpleXMLElement Object
I have a SimpleXMLElement Object that I want to import with PHP. But it

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.