dousi5501 2016-11-17 23:04
浏览 19
已采纳

解析Simplexml儿童的孩子

I've been trying to parse this data for a while now, but I don't really understand simpleXML and how it stores/retrieves. I have the following xml:

   <ListOrderItemsResult>
    <OrderItems>
      <OrderItem>
        <QuantityOrdered>1</QuantityOrdered>
        <Title>Organic Chamomile &amp; Lavender Shea Butter CP Soap Making Kit 2 Lbs.</Title>
        <ItemPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>29.99</Amount>
        </ItemPrice>
        <ItemTax>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>0.00</Amount>
        </ItemTax>
      </OrderItem>
      <OrderItem>
        <QuantityOrdered>1</QuantityOrdered>
        <Title>Eucalyptus &amp; Mint Organic Shea Butter CP Soap Making Kit 3 lbs.</Title>
        <ItemPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>32.99</Amount>
        </ItemPrice>
        <ItemTax>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>0.00</Amount>
        </ItemTax>
        </ShippingDiscount>
      </OrderItem>
    </OrderItems>
    <AmazonOrderId>12134</AmazonOrderId>
  </ListOrderItemsResult>

I'm trying to convert it into csv with each OrderItem being a row. The columns are: QuantityOrdered, Title, ItemPrice, ItemTax. So far I've managed to grab the everything but ItemPrice and ItemTax, because they have children nodes and I have no idea how to access them.

Here is my code:

    $OrderItem = $xmlDocs->xpath('//a:OrderItem'); 

foreach ($OrderItem as $n) {

  foreach ($AmazonOrderId as $t) {
    $row[]=$t;
  }
   // Iterate through each child of <OrderItem> node
  $child = $xml->xpath('//a:OrderItem['.$i.']/*'); 



  foreach ($child as $value) {
    $row[] = $value;
  }
  print_r($row);



   $fs = fopen('141.csv', 'a');
   fputcsv($fs, $row);      

   $row = [];
   // Clean out array for next <item> (i.e., row)
   $i++;            // Move to next <item> (i.e., node position)

}

fclose($fs);

I have it working, but it's since I can't figure out how to get to the child of the child, nothing shows up for ItemPrice and ItemTax.

TL;DR - I'm trying to cycle through each OrderItem node and get the values of each child node and if the child node has children, I only want to get the value of the node named 'Amount'.

  • 写回答

1条回答 默认 最新

  • dq23171 2016-11-18 01:17
    关注

    I figured out how to do it. I had to really break down simplexml to understand how the object layers so that I could figure out how to extract the data I wanted. Hopefully, this makes sense to someone, but when you're trying to extract a particular value from a SimpleXML object, you want it to read like this when you print_r():

       SimpleXMLElement Object
      (
          [0] => 0 //0 being the value you want - you can now add this to whatever variable you want once you have it down to this. 
       )
    

    Here was the code that did it:

        //Iterate through the first node <OrderItem>
    foreach ($OrderItem as $n) {           
    
      //Add the AmazonOrderId to the start of each line
      foreach ($AmazonOrderId as $t) {
        $row[]=$t;
      }
    
      $child = $xml->xpath('//a:OrderItem['.$i.']/*'); 
      // Iterate through each child of <OrderItem> node
      foreach ($child as $value) {
        //If the child has an attribute named <Amount>
        if($value->Amount[0]){
          //Iterate through and grab the value of <Amount> and post add to $row
          foreach ($value->Amount as $t) {
            $row[] = $t;
          } 
        } 
        //Else add the value to $row
        else { 
          $row[] = $value;
      }
    }
    
    
    $fs = fopen('141.csv', 'a');
    fputcsv($fs, $row);
    $row = [];
    $i++;  
    }
    fclose($fs); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c