Php 这些神秘字段在XML中的位置是什么?

Php 这些神秘字段在XML中的位置是什么?,php,xml,csv,Php,Xml,Csv,我从供应商那里下载了一个文件products.xml。我有一个脚本从这个文件中获取数据并将其放入csv文件中 因此,我正在查看这个脚本,其中似乎有从xml中提取的数据,我无法用任何xml编辑器看到这些数据。比如图像和类别。另一方面,我看到xml文件中的某些字段在应该放在csv中时没有放在csv中,例如产品名称 除了添加产品名称和描述之外,它做的一切都是正确的 这里是脚本将数据从xml抓取到数组中的地方 foreach($xmlData->children() as $product){ /

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

因此,我正在查看这个脚本,其中似乎有从xml中提取的数据,我无法用任何xml编辑器看到这些数据。比如图像和类别。另一方面,我看到xml文件中的某些字段在应该放在csv中时没有放在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文件中,我看不到任何对图像的引用,但这段代码为每个产品生成了正确的图像列表。这一切都很好,但我需要理解为什么会这样。一旦我理解了这一点,我可能就会明白为什么名称和产品描述部分不起作用

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
            )

    )

)
此外,他在产品描述中只使用了“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>";

我已经放弃了。我浏览了这个网站和网络,感觉自己什么都试过了。我不明白为什么在其他一切正常的情况下,“名称”和“描述”数据不会写入csv文件。xml文件中的名称和描述似乎非常简单。我知道这个脚本在一年多前就开始工作了,我不知道为什么它现在不能只为这两件事工作(

您想使用
LIBXML\u NOCDATA
。这对我来说很有用,可以使用您的示例数据:

DJ CATALOGS
some short description
some long description
输出:


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

您可能需要检查
类别
图像
属于
$product
所属的类。我不知道您的意思。我看不到xml中的任何类别或图像,这对我来说是个谜。我需要看看是否能找到更多关于xml格式的信息。对我来说,这似乎很直截了当我想我现在明白为什么我看不到所有的xml数据了。我仍在研究如何访问名称和描述信息。我想我很快就会找到答案的。伊姆索普的回答在这里非常有用:谢谢艾布拉。这确实给了我一个更好看的输出,但我仍然只得到名称和描述的空白列。我会继续努力的。
$xmlData = simplexml_load_file($xmlFileName, null, 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