2013-09-28 83 views
1

我想用Symfony2 DomCrawler從XML接收一些信息。對於培訓,我使用以下XML結構:http://www.w3schools.com/xpath/xpath_examples.aspSymfony2 DomCrawler返回空節點列表

我想寫進$履帶

每本書的書名這是我的代碼:

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\DomCrawler\Crawler; 

class ImportController extends Controller 
{ 
    public function indexAction($publisher) 
    { 
     $books = array(); 
     $bookstore = <<<'XML' 
      <bookstore> 

       <book category="COOKING"> 
        <title lang="en">Everyday Italian</title> 
        <author>Giada De Laurentiis</author> 
        <year>2005</year> 
        <price>30.00</price> 
       </book> 

       <book category="CHILDREN"> 
        <title lang="en">Harry Potter</title> 
        <author>J K. Rowling</author> 
        <year>2005</year> 
        <price>29.99</price> 
       </book> 

       <book category="WEB"> 
        <title lang="en">XQuery Kick Start</title> 
        <author>James McGovern</author> 
        <author>Per Bothner</author> 
        <author>Kurt Cagle</author> 
        <author>James Linn</author> 
        <author>Vaidyanathan Nagarajan</author> 
        <year>2003</year> 
        <price>49.99</price> 
       </book> 

       <book category="WEB"> 
        <title lang="en">Learning XML</title> 
        <author>Erik T. Ray</author> 
        <year>2003</year> 
        <price>39.95</price> 
       </book> 

      </bookstore> 
      XML; 
     //Crawl now 
     $crawler = new Crawler($bookstore);    
     $crawler->filterXPath('/bookstore/book/title')->text(); 
     return $this->render('souncImportBundle:Import:index.html.twig', array('publisher' => $publisher, 'books' => $books, 'msg' => $bookstore, 'crawler' => $crawler)); 
    } 
} 

這將返回以下異常:

當前節點列表爲空。 500內部服務器錯誤 - InvalidArgumentException

爲什麼它沒有辦法呢?

回答

0

我得到了同樣的錯誤信息,但我認爲在你的情況,你應該指定你想要的項目書:

$crawler->filterXPath("/bookstore/book*[name() = 'book' and (position() = 1)]/title")->text(); 

可以使用的Symfony \分量\ CssSelector \ CssSelector來創建Xpath的文件管理器。

你也可以得到元素像這樣我猜:

$counter = $crawler->filter('bookstore > book')->count(); 
for ($i=1; $i < $counter; $i++) 
{ 
echo $crawler->filter('bookstore > book:nth-child('.$i.') > title')->text(); 
} 
0

問題是與你的heredoc標註。您需要將第二個XML一直向上放在文檔的左側牆上;包含它的終結者在線之前不能有任何空格。我知道這聽起來很奇怪,它會弄亂你的縮進,但這是PHP長期以來的一個已知問題。因此,您的代碼應爲:

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\DomCrawler\Crawler; 

class ImportController extends Controller 
{ 
    public function indexAction($publisher) 
    { 
     $books = array(); 
     $bookstore = <<<'XML' 
      <bookstore> 

       <book category="COOKING"> 
        <title lang="en">Everyday Italian</title> 
        <author>Giada De Laurentiis</author> 
        <year>2005</year> 
        <price>30.00</price> 
       </book> 

       <book category="CHILDREN"> 
        <title lang="en">Harry Potter</title> 
        <author>J K. Rowling</author> 
        <year>2005</year> 
        <price>29.99</price> 
       </book> 

       <book category="WEB"> 
        <title lang="en">XQuery Kick Start</title> 
        <author>James McGovern</author> 
        <author>Per Bothner</author> 
        <author>Kurt Cagle</author> 
        <author>James Linn</author> 
        <author>Vaidyanathan Nagarajan</author> 
        <year>2003</year> 
        <price>49.99</price> 
       </book> 

       <book category="WEB"> 
        <title lang="en">Learning XML</title> 
        <author>Erik T. Ray</author> 
        <year>2003</year> 
        <price>39.95</price> 
       </book> 

      </bookstore> 
XML; 
     //Crawl now 
     $crawler = new Crawler($bookstore);    
     $crawler->filterXPath('/bookstore/book/title')->text(); 
     return $this->render('souncImportBundle:Import:index.html.twig', array('publisher' => $publisher, 'books' => $books, 'msg' => $bookstore, 'crawler' => $crawler)); 
    } 
} 
+0

您還會注意到,使用該語法時,StackOverflow的代碼編輯器無法正確突出顯示。 HEREDOC中的所有內容都被錯誤地突出顯示,但突出顯示在「XML」後面的分號後變回正確。 – PapaHotelPapa