为什么我的数组不能转换为XML?

时间:2014-03-15 22:24:33

标签: php xml arrays

我有一个数组($result)我试图转换并输出为XML,但它并没有发生在我身上。

任何想法可能都错了?

这里是output

<rss version="2.0">
    <script/>
    <script id="tinyhippos-injected"/>
    <channel>
        <item>
            <product_url/>
            <shop_name/>
            <photo_url/>
        </item>
    </channel>
</rss>

以下是代码:

function scrape($list_url, $shop_name, $photo_location, $photo_url_root, $product_location, $product_url_root, $was_price_location, $now_price_location, $gender, $country)
{

    header("Content-Type: application/xml; charset=UTF-8");

    $xml = new SimpleXMLElement('<rss/>');
    $xml->addAttribute("version", "2.0");
    $channel = $xml->addChild("channel");

    $html = file_get_contents($list_url);
    $doc = new DOMDocument();
    libxml_use_internal_errors(TRUE);

    if (!empty($html))
    {
        $doc->loadHTML($html);
        libxml_clear_errors(); // remove errors for yucky html
        $xpath = new DOMXPath($doc);
        /* FIND LINK TO PRODUCT PAGE */
        $products = array();
        $entries = array();
/* product_url */
        $row = $xpath->query($product_location);
        if ($row->length > 0)
        {
            foreach ($row as $location)
            {
                $product_url[] = $product_url_root . $location->getAttribute('href');
            }
        }
/* photo_url */
        $imgs = $xpath->query($photo_location);
        if ($imgs->length > 0)
        {
            foreach ($imgs as $img)
            {
                $photo_url[] = $photo_url_root . $img->getAttribute('src');
            }
        }
        $result = array();
        foreach ($product_url as $i => $val)
        {
            $result[] = array(
                'product_url' => $val,
                'shop_name' => $shop_name,
                'photo_url' => $photo_url[$i]
            );
        }

        //print_r($result);

        $item = $channel->addChild("item");
        $item->addChild("product_url", $result['product_url']);
        $item->addChild("shop_name", $result['shop_name']);
        $item->addChild("photo_url", $result['photo_url']);

    }
    echo $xml->asXML();
}

0 个答案:

没有答案