1

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

为什么它不起作用?

4

2 回答 2

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();
} 
于 2014-07-15T10:15:55.410 回答
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));
    }
}
于 2016-03-11T21:29:24.610 回答