普通网友 2013-08-10 15:28
浏览 22
已采纳

XML解析和foreach结构

I'm new to XML parsing but not new to php and html. I need to parse the following XML structure:

<er_data service="pl">
    <manufacturer_item>
        <item_id></item_id>
        <model></model>
        <manufacturer></manufacturer>
        <product_name></product_name>
        <description></description>
        <datasheeturl/>
        <image_url/>
        <map></map>
    </manufacturer_item>
    <dealers>
        <dealer id="111">
            <dealer_logo>http:/source.com/images/vendors/90x60/111.gif</dealer_logo>
            <dealer_name></dealer_name>
            <dealer_url></dealer_url>
        </dealer>
        <dealer id="222">
            <dealer_logo>http:/source.com/images/vendors/90x60/222.gif</dealer_logo>
            <dealer_name></dealer_name>
            <dealer_url></dealer_url>
        </dealer>
    </dealers>
    <dealer_item>
        <dealer_id>111</dealer_id>
        <dealer_sku></dealer_sku>
        <model></model>
        <manufacturer></manufacturer>
        <name></name>
        <price></price>
        <availability></availability>
        <image_url></image_url>
        <buy_url>http://source.com/c/?nVID=111&rsSku=321321</buy_url>
    </dealer_item>
    <dealer_item>
        <dealer_id>222</dealer_id>
        <dealer_sku></dealer_sku>
        <model></model>
        <manufacturer></manufacturer>
        <name></name>
        <price></price>
        <availability></availability>
        <image_url></image_url>
        <buy_url>http://source.com/c/?nVID=222&rsSku=654654</buy_url>
    </dealer_item>
    <loggingpixel></loggingpixel>
</er_data>

and create a foreach that outputs html like this:

<tr>
    <td><img src=$dealer_logo></td>
<td><a href=$buy_url>Buy Online</a></td>
</tr>

For brevity, I'm not putting all the things I've tried. There will be one manufacturer item and it will be a constant. There will be multiple dealer id (s) and each dealer could have multiple dealer_item (s) but will always have at least one. I don't know the constructs to use to get the things I need. The dealer_logo comes out of dealers and the buy_url comes out of dealer_items. Note the logo URL will have the dealer id number in it.

For brevity, I won't show all the newbie things I've tried. Basically, I am using a structure like this for the reading and looping:

$retailers = simplexml_load_file("http://source.com/xml.asp");

foreach ($retailers->dealer_items as $storeinfo):
endforeach;

Any help would be greatly appreciated! Sorry that the XML is not indented properly. Karen edit MrCode- I'm still getting nothing. The XML file is under someone else's control but when I open it in Safari and save it to disk I see &. I also changed the URL that I feed it to have the & but still nothing. Here is the code exactly as I have it currently:

<table class="gridtable">
<tr>
    <th>Retailer</th>
    <th>In Stock</th>
    <th></th>
    </tr>

<?php   
$retailers = simplexml_load_string('http://pg.links.channelintelligence.com/pages/plxml.asp?sSKU=633472600401&amp;nRGID=4938');
$dealers = array();
foreach($retailers->dealers->dealer as $dealer)
{
$dealers[(int)$dealer->attributes()->id] = array('dealer_logo' => (string)$dealer->dealer_logo);
}
?>

<?php foreach($retailers->dealer_item as $item): ?>
<tr>
<td><img src="<?php echo $dealers[(int)$item->dealer_id]['dealer_logo']; ?>"></td>
<td>Yes</td>
<td><a href="<?php echo (string)$item->buy_url; ?>">Buy Online</a></td>
</tr>
<?php endforeach; ?>

</table>

edit (strugging with formatting on the forum) *The XML file is under someone else's control but when I open it in Safari and save it to disk I see the encoded version of ampersand.

Edit to post errors: [Mon Aug 12 20:32:06 2013] [error] [client 204.181.48.224] PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/sites/staging.clearblueeasy.com/website/m/advanced-digital-ovulation-test-buyonline.php on line 32 [Mon Aug 12 20:32:06 2013] [error] [client 204.181.48.224] PHP Warning: simplexml_load_string(): http://pg.links.channelintelligence.com/pages/plxml.asp?nrgid=4938&ssku=63347260 in /home/sites/staging.clearblueeasy.com/website/m/advanced-digital-ovulation-test-buyonline.php on line 32 [Mon Aug 12 20:32:06 2013] [error] [client 204.181.48.224] PHP Warning: simplexml_load_string(): ^ in /home/sites/staging.clearblueeasy.com/website/m/advanced-digital-ovulation-test-buyonline.php on line 32 [Mon Aug 12 20:32:06 2013] [error] [client 204.181.48.224] PHP Warning: Invalid argument supplied for foreach() in /home/sites/staging.clearblueeasy.com/website/m/advanced-digital-ovulation-test-buyonline.php on line 34 [Mon Aug 12 20:32:06 2013] [error] [client 204.181.48.224] PHP Warning: Invalid argument supplied for foreach() in /home/sites/staging.clearblueeasy.com/website/m/advanced-digital-ovulation-test-buyonline.php on line 40

  • 写回答

1条回答 默认 最新

  • dongtiao5094 2013-08-10 16:14
    关注

    Because you have the dealers defined separately in the XML, you will need to first extract the dealers, so that you can lookup the dealer_logo based on the ID in the dealer_item:

    <?php
    // assume $retailers is the xml object
    $dealers = array();
    foreach($retailers->dealers->dealer as $dealer)
    {
        $dealers[(int)$dealer->attributes()->id] = array('dealer_logo' => (string)$dealer->dealer_logo);
    }
    ?>
    
    <?php foreach($retailers->dealer_item as $item): ?>
    <tr>
        <td><img src="<?php echo $dealers[(int)$item->dealer_id]['dealer_logo']; ?>"></td>
        <td><a href="<?php echo (string)$item->buy_url; ?>">Buy Online</a></td>
    </tr>
    <?php endforeach; ?>
    

    Edit: Codepad Demo

    Make sure that your XML is valid. Take a look at your URLs in the XML, they contain raw & which is not valid. You need to encode them into &amp;. In the demo, I have changed them so you can see the code working.

    Edit 2: final code

    <?php
    
    $retailers = simplexml_load_file('http://pg.links.channelintelligence.com/pages/plxml.asp?sSKU=633472600401&nRGID=4938');
    
    $dealers = array();
    foreach($retailers->dealers->dealer as $dealer)
    {
        $dealers[(int)$dealer->attributes()->id] = array('dealer_logo' => (string)$dealer->dealer_logo);
    }
    
    ?>
    
    <?php foreach($retailers->dealer_items->dealer_item as $item): ?>
    <tr>
        <td><img src="<?php echo $dealers[(int)$item->dealer_id]['dealer_logo']; ?>"></td>
        <td><a href="<?php echo (string)$item->buy_url; ?>">Buy Online</a></td>
    </tr>
    <?php endforeach; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应