Symfony2 DomCrawler返回空节点列表

时间:2013-09-28 14:42:46

标签: symfony xpath

我正在尝试使用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

为什么不这样做?

2 个答案:

答案 0 :(得分:0)

我收到了相同的错误消息,但我认为在您的情况下,您应该指定您想要的项目簿:

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

您可以使用Symfony \ Component \ 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();
} 

答案 1 :(得分: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));
    }
}