如何遍历远程XML文件并生成含所有唯一标签的PHP数组
获取XML中所有唯一标签的PHP实现方案
需求说明
需要从远程XML文件生成一个包含所有唯一标签名的PHP数组,由于XML的深度和结构未知,递归函数是最佳实现方式。
现有代码
$xml = file_get_contents('https://sxy.bg/test.xml'); $xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
目标XML内容
<rss version="2.0"> <channel> <title name="product">Test Store</title> <link type="url">https://www.sxy.bg/</link> <description encoding="utf-8">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");
解决方案
实现思路
- 编写递归函数遍历所有SimpleXMLElement节点,收集每个节点的标签名(含命名空间前缀)
- 使用数组去重函数确保标签唯一
- 整理结果为目标格式
完整代码实现
// 获取远程XML内容并解析 $xmlContent = file_get_contents('https://sxy.bg/test.xml'); $xml = simplexml_load_string($xmlContent, 'SimpleXMLElement', LIBXML_NOCDATA); // 递归收集标签名的函数 function collectTags($node, &$tags) { // 获取当前节点的标签名(含命名空间) $tagName = $node->getName(); $tags[] = $tagName; // 遍历所有子节点,递归处理 foreach ($node->children() as $child) { collectTags($child, $tags); } } // 初始化标签数组 $tags = []; // 从根节点开始遍历 collectTags($xml, $tags); // 去重并重置数组索引 $uniqueTags = array_values(array_unique($tags)); // 输出结果(或按需使用) print_r($uniqueTags);
代码说明
collectTags函数通过递归遍历每个节点,将标签名存入引用传递的数组中array_unique用于去除重复的标签名array_values重置数组索引,确保输出为连续的数字索引数组
执行上述代码后,将得到与期望格式一致的唯一标签数组。
内容的提问来源于stack exchange,提问作者Europeuser
相关产品推荐
相关产品推荐

