我正在尝试使用 Symfony2 DomCrawler 从 XML 接收一些信息。对于培训,我使用以下 XML 结构:http ://www.w3schools.com/xpath/xpath_examples.asp
我想将每本书的标题写入 $crawler
这是我的代码:
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
为什么它不起作用?