Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 DomCrawler and FB2 book format parser
    text
    copied!<p>All!</p> <p>How do I parse correctly described XML file with Symfony2 DomCrawler component?</p> <p>I need to split all the sections and collect an internal tags (epigraph, p, poem etc.) with the current section together which belongs to this section only.</p> <p>I've standard FB2 book XML format described below:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink"&gt; &lt;description&gt;&lt;/description&gt; &lt;body&gt; &lt;section&gt; &lt;title&gt;&lt;p&gt;&lt;strong&gt;Level 1, section 1&lt;/strong&gt;&lt;/p&gt;&lt;/title&gt; &lt;section&gt; &lt;title&gt;&lt;p&gt;&lt;strong&gt;Level 2, section 2&lt;/strong&gt;&lt;/p&gt;&lt;/title&gt; &lt;section&gt; &lt;title&gt;&lt;p&gt;&lt;strong&gt;Level 3, section 3&lt;/strong&gt;&lt;/p&gt;&lt;/title&gt; &lt;p&gt;Level 3, section 3, paragraph 1&lt;/p&gt; &lt;poem&gt; &lt;stanza&gt; &lt;v&gt;bla-bla-bla 1&lt;/v&gt; &lt;v&gt;bla-bla-bla 2&lt;/v&gt; &lt;v&gt;bla-bla-bla 3&lt;/v&gt; &lt;/stanza&gt; &lt;/poem&gt; &lt;p&gt;Level3, section 3, paragraph 2&lt;/p&gt; &lt;subtitle&gt;&lt;strong&gt;x x x&lt;/strong&gt;&lt;/subtitle&gt; &lt;/section&gt; &lt;section&gt; &lt;title&gt;&lt;p&gt;&lt;strong&gt;Level 3, section 4&lt;/strong&gt;&lt;/p&gt;&lt;/title&gt; &lt;p&gt;Level 3, section 4, paragraph 1&lt;/p&gt; &lt;p&gt;Level 3, section 4, paragraph 2&lt;/p&gt; &lt;subtitle&gt;&lt;strong&gt;x x x&lt;/strong&gt;&lt;/subtitle&gt; &lt;/section&gt; &lt;section&gt; &lt;title&gt;&lt;p&gt;&lt;strong&gt;Level 3, section 5&lt;/strong&gt;&lt;/p&gt;&lt;/title&gt; &lt;p&gt;Level 3, section 5, paragraph 1&lt;/p&gt; &lt;p&gt;Level 3, section 5, paragraph 2&lt;/p&gt; &lt;p&gt;Level 3, section 5, paragraph 3&lt;/p&gt; &lt;empty-line/&gt; &lt;subtitle&gt;This file was created&lt;/subtitle&gt; &lt;subtitle&gt;with BookDesigner program&lt;/subtitle&gt; &lt;subtitle&gt;bookdesigner@the-ebook.org&lt;/subtitle&gt; &lt;subtitle&gt;22.04.2004&lt;/subtitle&gt; &lt;/section&gt; &lt;/section&gt; &lt;/section&gt; &lt;/body&gt; &lt;/FictionBook&gt; </code></pre> <p>The code below do not work, so could somebody help me to solve this? Btw, title parsed correctly... but section's tags not...</p> <pre><code>private function loadBookSections(Crawler $crawler) { $sections = $crawler-&gt;filter('section')-&gt;each(function(Crawler $node) { $c = $node-&gt;filter('section')-&gt;reduce(function(Crawler $node, $i) { return ($i == 0); }); return array( 'title' =&gt; $node-&gt;filter('title')-&gt;text(), 'inner' =&gt; $c-&gt;html(), ); }); echo "*******************************************\n"; foreach($sections as $section ) { echo "&gt;&gt;&gt; ".$section['title']."\n"; echo "!!! ".$section['inner']."\n"; } } </code></pre> <p>And Thanks for help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload