我想创建一个php数组,其中包含远程XML文件中的所有XML标记。XML文件的深度和结构将是未知的,所以这就是为什么递归函数可能是最好的解决方案。无论如何,我将添加一个样本只是为了研究的情况。以下是我目前为止的代码:
$xml = file_get_contents('https://sxy.bg/test.xml');
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
文件中的xml为:
$xml = '<rss version="2.0">
<channel>
<title>Test Store</title>
<link>https://www.sxy.bg/</link>
<description>SXY</description>
<item>
<g:id>8797</g:id>
<g:title>Product Title 8797</g:title>
<g:description>Product description 8797</g:description>
<g:link>https://www.sxy.bg/store/8797</g:link>
<g:image_link>https://www.sxy.bg/images/store/8797.jpg</g:image_link>
<g:brand>Olight</g:brand>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>99.00 EUR</g:price>
<g:shipping>
<g:country>BG</g:country>
<g:service>Standard</g:service>
<g:price>4.95 EUR</g:price>
</g:shipping>
<g:google_product_category>Auto</g:google_product_category>
<g:custom_label_0>Hot Offers</g:custom_label_0>
</item>
<item>
<g:id>8813</g:id>
<g:title>Product Title 8813</g:title>
<g:description>Product description 8813</g:description>
<g:link>https://www.sxy.bg/store/8813</g:link>
<g:image_link>https://www.sxy.bg/images/store/8813.jpg</g:image_link>
<g:brand>Olight</g:brand>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>305.00 EUR</g:price>
<g:shipping>
<g:country>BG</g:country>
<g:service>Standard</g:service>
<g:price>4.95 EUR</g:price>
</g:shipping>
<g:google_product_category>Auto</g:google_product_category>
<g:custom_label_0>Hot Offers</g:custom_label_0>
</item>
<item>
<g:id>8809</g:id>
<g:title>Product Title 8809</g:title>
<g:description>Product description 8809</g:description>
<g:link>https://www.sxy.bg/store/8809</g:link>
<g:image_link>https://www.sxy.bg/images/store/8809.jpg</g:image_link>
<g:brand>Olight</g:brand>
<g:condition>new</g:condition>
<g:availability>in stock</g:availability>
<g:price>375.00 EUR</g:price>
<g:shipping>
<g:country>BG</g:country>
<g:service>Standard</g:service>
<g:price>4.95 EUR</g:price>
</g:shipping>
<g:google_product_category>Auto</g:google_product_category>
<g:custom_label_0>Hot Offers</g:custom_label_0>
</item>
</channel>
</rss>';
所需的数组格式为:
array("channel", "title", "Link","description", "item", "g:id","g:title", "g:description","g:link","g:image_link","g:brand","g:condition", "g:availability","g:price","g:shipping","g:country","g:service","g:google_product_category","g:custom_label_0");
数组应仅由唯一标记组成。完成这项任务的最佳方法是什么?谢谢你的时间
1条答案
按热度按时间pbossiut1#
如果您只是想要所有唯一的标记名,那么您可以使用一个简单的正则表达式