【问题标题】:trying to read an attribute from an xml file试图从 xml 文件中读取属性
【发布时间】:2021-08-08 08:02:20
【问题描述】:

我一直在从 xml 文件中读取属性。

xml 文件的示例如下:

<products products_num="358">
  <product product_id="71166" type="main" product_counter="1">
   <product_id>71122</product_id>
   <sku>TN245</sku>
   <model>37TN245Y</model>
   <name>PRODUCT NAME</name>
   <manufacturer>OEM</manufacturer>
   <url>http://www.mysite.xx/index.php?route=product/product&amp;product_id=71122</url>
   <product_categories prod_cat_num="1">
    <category_path category_id="48">CATEGORY -&gt; CATEGORY</category_path>
   </product_categories>
   <mpn>-</mpn>
   <barcode>-</barcode>
   <date_added>2021-03-17 15:16:26</date_added>
   <last_modified>2021-03-17 15:16:26</last_modified>
   <description><![CDATA[DESCRTION]]></description>
   <images>
    <image_url type="main">http://www.mysyte.xx/image/catalog/product/10061.jpg</image_url>
   </images>
   <price tax_rate="24%" customer_perc="50.00%" currency="EUR">
    <price_original>7.9300</price_original>
    <price_with_perc>11.8950</price_with_perc>
    <price_with_tax>14.7498</price_with_tax>
   </price>
   <instock>Y</instock>
   <shipping>Y</shipping>
   <minimum_quantity>1</minimum_quantity>
   <availability status="In Stock" quantity="32" id="7"/>
  </product>
 </products>

我正在使用以下代码

$family = $this->getCurrentElement($object->product_categories->product_categories);
$this->_product['attribute'] = $family['category_id'];

我可以读取 xml 的所有其他元素,但不能读取 category_id。所以我试图捕获字符串 category_id 没有运气。

有什么帮助吗?

谢谢

【问题讨论】:

  • 所以您要更新 xml?
  • 试试$family-&gt;category_path['category_id'](因为$family包含product_categories并且id在里面的category_path元素上)
  • $this-&gt;getCurrentElement 长什么样子?

标签: php xml xml-parsing


【解决方案1】:

我尝试了不同格式的代码,如果没问题你可以关注!

<?php
$string = '<products products_num="358">
  <product product_id="71166" type="main" product_counter="1">
   <product_id>71122</product_id>
   <sku>TN245</sku>
   <model>37TN245Y</model>
   <name>PRODUCT NAME</name>
   <manufacturer>OEM</manufacturer>
   <url>http://www.mysite.xx/index.php?route=product/product&amp;product_id=71122</url>
   <product_categories prod_cat_num="1">
    <category_path category_id="48">CATEGORY -&gt; CATEGORY</category_path>
   </product_categories>
   <mpn>-</mpn>
   <barcode>-</barcode>
   <date_added>2021-03-17 15:16:26</date_added>
   <last_modified>2021-03-17 15:16:26</last_modified>
   <description><![CDATA[DESCRTION]]></description>
   <images>
    <image_url type="main">http://www.mysyte.xx/image/catalog/product/10061.jpg</image_url>
   </images>
   <price tax_rate="24%" customer_perc="50.00%" currency="EUR">
    <price_original>7.9300</price_original>
    <price_with_perc>11.8950</price_with_perc>
    <price_with_tax>14.7498</price_with_tax>
   </price>
   <instock>Y</instock>
   <shipping>Y</shipping>
   <minimum_quantity>1</minimum_quantity>
   <availability status="In Stock" quantity="32" id="7"/>
  </product>
 </products>';                                                                       
                                          
$xml = simplexml_load_string($string);
echo($xml->product->product_id)."<br/>";
echo($xml->product->product_categories->category_path[@category_id])."<br/>";
echo($xml->product->sku)."<br/>";
?>

【讨论】:

  • xml 是外部的。我试图修改这样的代码 $family = $this->getCurrentElement($object->product_categories->category_path); $this->_product['attribute'] = $family['category_id'];但没有快乐......
  • 我觉得奇怪的是使用 $family = $this->getCurrentElement($object->product_categories); $this->_product['category_id'] = $family['prod_cat_num'];作品。但是 $family = $this->getCurrentElement($object->product_categories->category_path); $this->_product['category_id'] = $family['category_id'];没有
  • $family = $this->getCurrentElement($object->product->product_categories->category_path[@category_id]);?你能从你身边检查一下吗?我观察者是 _product 在你的 xml 中不可用?
  • 嗨,不工作我收到通知:试图获取非对象的属性
  • 试试这个代码:$object = simplexml_load_file('file.xml'); $family = $object->产品; echo($family->product_id)."
    "; echo($family->model)."
    "; echo($family->product_categories[@prod_cat_num])."
    "; echo($family->product_categories->category_path[@category_id])."
    ";回声($family->sku);
猜你喜欢
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-21
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多