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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:43:20+00:00 2026-05-13T21:43:20+00:00

I need some help on the SimpleXML calls for a recursive function that lists

  • 0

I need some help on the SimpleXML calls for a recursive function that lists the elements name and attributes. Making a XML config file system but each script will have it’s own config file as well as a new naming convention. So what I need is an easy way to map out all the elements that have attributes, so like in example 1 I need a simple way to call all the processes but I don’t know how to do this without hard coding the elements name is the function call. Is there a way to recursively call a function to match a child element name? I did see the xpath functionality but I don’t see how to use this for attributes.

Also does the XML in the examples look correct? can I structure my XML like this?

Example 1:

<application>
  <processes>
    <process id="123" name="run batch A" />
    <process id="122" name="run batch B" />
    <process id="129" name="run batch C" />
  </processes>
  <connections>
    <databases>
      <database usr="test" pss="test" hst="test" dbn="test" />
    </databases>
    <shells>
      <ssh usr="test" pss="test" hst="test-2" />
      <ssh usr="test" pss="test" hst="test-1" />
    </shells>
  </connections>
</application>

Example 2:

<config>
  <queues>
    <queue id="1" name="test" />
    <queue id="2" name="production" />
    <queue id="3" name="error" />
  </queues>
</config>

Pseudo code:

// Would return matching process id
getProcess($process_id) {
  return the process attributes as array that are in the XML
}

// Would return matching DBN (database name)
getDatabase($database_name) {
  return the database attributes as array that are in the XML
}

// Would return matching SSH Host
getSSHHost($ssh_host) {
  return the ssh attributes as array that are in the XML
}

// Would return matching SSH User
getSSHUser($ssh_user) {
  return the ssh attributes as array that are in the XML
}

// Would return matching Queue 
getQueue($queue_id) {
  return the queue attributes as array that are in the XML
}

EDIT:

Can I pass two parms? on the first method you have suggested @Gordon

I just got it, thnx, see below

public function findProcessById($id, $name)
{
    $attr = false;
    $el = $this->xml->xpath("//process[@id='$id'][@name='$name']"); // How do I also filter by the name?
    if($el && count($el) === 1) {
        $attr = (array) $el[0]->attributes();
        $attr = $attr['@attributes'];
    }
    return $attr;
}
  • 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-13T21:43:20+00:00Added an answer on May 13, 2026 at 9:43 pm

    The XML looks good to me. The only thing I wouldn’t do is making name an attribute in process, because it contains spaces and should be a textnode then (in my opinion). But since SimpleXml does not complain about it, I guess it boils down to personal preference.

    I’d likely approach this with a DataFinder class, encapsulating XPath queries, e.g.

    class XmlFinder
    {
        protected $xml;
        public function __construct($xml)
        {
            $this->xml = new SimpleXMLElement($xml);
        }
        public function findProcessById($id)
        {
            $attr = false;
            $el = $this->xml->xpath("//process[@id='$id']");
            if($el && count($el) === 1) {
                $attr = (array) $el[0]->attributes();
                $attr = $attr['@attributes'];
            }
            return $attr;
        }
        // ... other methods ...
    }
    

    and then use it with

    $finder = new XmlFinder($xml);
    print_r( $finder->findProcessById(122) );
    

    Output:

    Array
    (
        [id] => 122
        [name] => run batch B
    )
    

    XPath tutorial:

    • http://www.w3schools.com/XPath/default.asp

    An alternative would be to use SimpleXmlIterator to iterate over the elements. Iterators can be decorated with other Iterators, so you can do:

    class XmlFilterIterator extends FilterIterator
    {
        protected $filterElement;
        public function setFilterElement($name)
        {
            $this->filterElement = $name;
        }
        public function accept()
        {
            return ($this->current()->getName() === $this->filterElement);
        }
    }
    
    $sxi = new XmlFilterIterator(
               new RecursiveIteratorIterator( 
                   new SimpleXmlIterator($xml)));
    
    $sxi->setFilterElement('process');
    
    foreach($sxi as $el) {
        var_dump( $el ); // will only give process elements
    }
    

    You would have to add some more methods to have the filter work for attributes, but this is a rather trivial task.

    Introduction to SplIterators:

    • http://www.phpro.org/tutorials/Introduction-to-SPL.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer it turns out this was due to my firewall (firestarter)… May 13, 2026 at 10:47 pm
  • Editorial Team
    Editorial Team added an answer It's easier than you think: >>> import numpy >>> A… May 13, 2026 at 10:47 pm
  • Editorial Team
    Editorial Team added an answer There is no difference. In the first one, the compiler… May 13, 2026 at 10:47 pm

Related Questions

I have started to use the simplexml function that seems to work out better
Today I am looking into how to make a simple XML parser in Cocoa
I have a simple xml document that looks like the following snippet. I need
I desperately need some help on this one. I've created a <script> that closely
I need some help tracking down a bit of nitty gritty information on the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.