我想用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
爲什麼它沒有辦法呢?
您還會注意到,使用該語法時,StackOverflow的代碼編輯器無法正確突出顯示。 HEREDOC中的所有內容都被錯誤地突出顯示,但突出顯示在「XML」後面的分號後變回正確。 – PapaHotelPapa