这些神秘字段位于XML中的什么位置


Where are these mystery fields located in this XML?

我从供应商那里下载了一个文件products.xml。我有一个脚本从这个文件中获取数据,并把它放在一个csv文件中。

所以我正在看这个脚本,似乎有数据从xml,我不能看到任何xml编辑器。比如图片和分类。而另一方面,我看到xml文件中的某些字段在应该放入csv时没有放入,例如产品名称。

除了添加产品名称和描述之外,其他都做得很好。

这里是脚本从xml中抓取数据到数组的地方。

foreach($xmlData->children() as $product){
// Create an array which holds all of the data merged from the xml file
    $filteredData = array('code' => (string)$product->sku
                     ,'name' => (string)$product->name
                     ,'brand' => (string)$product->manufacturer
                     ,'description' => (string)$product->long_description
                     ,'costPrice' => (string)$product->price
                     ,'price' => (((string)$product->price) * 2)
                     ,'freeShipping' => 0
                     ,'weight' => (string)$product->weight
                     ,'allowPurchases' => 1
                     ,'productVisible' => 1
                     ,'productAvailability' => 'Ships within 48 Hours'
                     ,'trackInventory' => 1
                     ,'stockLevel' => (string)$product->stock_quantity
                     ,'lowStockLevel' => 1
                     ,'productCondition' => 'New'
                     ,'upc' => (string)$product->barcode
                     ,'category' => '');
// First category name goes to category details field
// If more than one category name exists, last category name goes to category field
$filteredData['categoryDetails'] = (string)$product->categories->category[0];
$categoryCount = count($product->categories->children());
if($categoryCount > 1){
    $filteredData['category'] = (string)$product->categories->category[$categoryCount - 1];
}
// Product image links must be appended to: http://images.xyz.com/product_images
$staticLinkToPrepend = 'http://images.xyz.com/product_images';
$filteredData['images'] = array();
foreach($product->images->children() as $image){
    $filteredData['images'][] = $staticLinkToPrepend . ((string)$image);
}

在products.xml文件中,我没有看到任何关于类别的引用,但是所编写的代码在csv文件中生成了正确的类别。在xml文件中,我没有看到任何对图像的引用,但是这段代码为每个产品生成了正确的图像清单。这一切都很好,但我需要明白为什么会这样。一旦我理解了这一点,我就可能明白为什么名称和产品描述部分不起作用了。

他也只用"description"作为产品描述,但在xml文件中,它有长描述和短描述。这两个都不行。名称是在xml文件中列出的,所以我不知道为什么它不工作。

Array
(
[product] => Array
    (
        [0] => Array
            (
                [@attributes] => Array
                    (
                        [sku] => CATDJ
                        [type] => BR
                        [vendor] => DJ
                        [active] => 0
                        [on_sale] => 0
                        [discountable] => 0
                    )
                [name] => DJ CATALOGS
                [short_description] => Array
                    (
                    )
                [long_description] => Array
                    (
                    )
                [price] => 1.5
                [stock_quantity] => 0
                [release_date] => 2003-05-06T00:00:00-04:00
                [barcode] => 782421712315
            )
        [1] => Array
            (
                [@attributes] => Array
                    (
                        [sku] => CATPD
                        [type] => DISC
                        [vendor] => PIEDR
                        [active] => 0
                        [on_sale] => 0
                        [discountable] => 0
                    )
                [name] => PIE DRUM CATALOG
                [short_description] => Array
                    (
                    )
                [long_description] => Array
                    (
                    )
                [price] => 1.5
                [stock_quantity] => 0
                [release_date] => 2003-05-06T00:00:00-04:00
                [barcode] => 603912241230
            )
    )
)

我只是把xml文件压缩到只有两个产品。我使用下面的代码来得到上面的内容。

if(!file_exists($xmlFileName)){
die('* XML file path is incorrect.  Please change and try again.');
}
$xmlData = simplexml_load_file($xmlFileName);
$json = json_encode($xmlData);
$array = json_decode($json,TRUE);
print "<pre>";
print_r($array);
print "</pre>";

所以"name"在那里,但它不会出现在最终的csv中。Long_description和short_description在那里,但使用上面的方法看起来是空的,但它们肯定在XML文件中带有文本值。

我能够基于这个脚本为我的Share-A-Sale feed创建另一个脚本,但该数据来自数据库,我没有问题。但我已经在这里呆了两个星期了。我现在正在重塑我的网站,这就是为什么我首先需要它。

这是xml文件中的样例条目。

<products>
<product sku="CATDJ" type="BR" vendor="DJ" active="0" on_sale="0" discountable="0">
<name>DJ CATALOGS</name>
<short_description><![CDATA[some short description]]></short_description>
<long_description><![CDATA[some long description]]></long_description>
<price>1.5</price>
<stock_quantity>0</stock_quantity>
<release_date>2003-05-06T00:00:00-04:00</release_date>
<barcode>782421712315</barcode>
</product>
</products>

更新:现在我真的很困惑。我从备份中找出了一个旧版本的products.xml文件,看看供应商是否更改了xml文件。这是非常不同的,我可以看到更多的字段(见下文),但我仍然感到困惑的是,当前的产品文件没有列出图像和类别数据,如何仍然可以产生csv文件中的数据。

 <product active="1" on_sale="0" discountable="0">
 <sku>AL1012XO</sku>
 <name>
  <![CDATA[LACE UP GLOVES]]>
</name>
<description>
  <![CDATA[Able to be adjusted as tight as you desire with a lace-and-grommet closure, they extend over the tops of the hands and have a built-in finger loop for a sleek look.]]>
</description>
<keywords></keywords>
<price>10.100000</price>
<stock_quantity>34</stock_quantity>
<reorder_quantity>0</reorder_quantity>
<height>0.000000</height>
<length>0.000000</length>
<diameter>0.000000</diameter>
<weight>0.000000</weight>
<color>Black</color>
<material>cotton</material>
<barcode>883125011270</barcode>
<release_date>2008-11-10</release_date>
<images>
  <image>/AL1012XO/AL1012XO.jpg</image>
  <image>/AL1012XO/AL1012XOB.jpg</image>
</images>
<categories>
  <category code="166" video="0" parent="172">Clothing</category>
  <category code="172" video="0" parent="">Clothing</category>
  <category code="641" video="0" parent="172">Accessories</category>
  <category code="167" video="0" parent="1001">Costumes</category>
</categories>
<manufacturer code="AL" video="0">Golvers</manufacturer>
<type code="AC" video="0">AC</type>

更新:我想我快到了。我仍然不知道为什么我不能看到xml文件中的所有内容,但它仍然存在。csv就像我想要的一样,但我仍然无法在csv文件中获得名称和描述。值得注意的是,名称和描述的引用是正确的,并且使用print_r打印数据很好,但似乎问题是将数据放入csv文件中。看来这两个字段是唯一两个与[CDATA],所以我正在研究为什么CDATA字段不使其进入csv文件。我现在也在用这个

$xmlData = simplexml_load_file($xmlFileName, null, LIBXML_NOCDATA);

我放弃了。我已经搜索了这个网站和网络,感觉我已经尝试了所有的东西。我不明白为什么"名称"answers"描述"数据不会被写入csv文件时,一切都很好。xml文件中的名称和描述看起来非常直接。我知道这个脚本在一年前是有效的,我不知道为什么它现在不能只适用于这两个东西。(

您想使用LIBXML_NOCDATA。这适用于我使用您的示例数据:

$xmlData = simplexml_load_string($xml, null, LIBXML_NOCDATA);    
echo $xmlData->product->name."'n";
echo $xmlData->product->short_description."'n";
echo $xmlData->product->long_description."'n";
输出:

DJ CATALOGS
some short description
some long description

我的供应商要把我累死了。它们有3个不同的xml链接,都声称是整个目录。他们都不是。我是从一个旧的链接上拉出来的。我仍然不知道为什么它不把名称和描述放在csv中,因为这些字段都在那里。但是我使用了上面列出的那个,我觉得它有隐藏字段(它没有),是的,那个缺少了大多数有用的产品数据。我至少去看了另一个xml文件,他们说有图像,它有我需要的所有信息。所有的痛苦都是徒劳的。:)