Lajdák Marek
Lajdák Marek

Reputation: 3069

Symfony crawler get child nodes from getted childs in foreach?

I am working with Symfony's dom crawler.

XML looks like:

<Tours>
    <Tour id="1">
        <Termins>
            <Termin>
            ...

And i have working code:

        $crawler = new Crawler($xmlData);

        foreach ($crawler->children() as $domElement) {

            $tourId = $domElement->getAttribute('id');
            $tours = $crawler->filter('Tours Tour[id="'.$tourId .'"] Termins')->children();

But i want find way to working with crawler like this: (Main point is without selectors from 'top' but from element witch is in foreach already).

        foreach ($crawler->children() as $tour) {
            foreach($tour->first('Termins')->children() as $termin)

Upvotes: 0

Views: 1922

Answers (1)

msg
msg

Reputation: 8161

At this moment, when you iterate over a Crawler you get an array of DOMNode. You can then create a new crawler for that subdocument.

foreach ($crawler->filter('tour') as $tour) {
    $tourCrawler = new Crawler($tour);
    $tourCrawler->attr('id'); // 1...
    foreach ($tourCrawler->filter('termin') as $termin) {
        // Access nodes or create further crawlers
    } 
}

Upvotes: 2

Related Questions