如何在php中读取高级xml文件?

时间:2018-03-30 12:57:28

标签: php xml xml-parsing

如何从xml文件下面检索图片网址? 我能够以数组或对象格式检索urlloc lastmodchangefreqpriority。 但是图像,标题,标题不是。 请告诉我哪种解决方案存在?

我能够检索下面的xml对象。

    SimpleXMLElement Object
(
    [url] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [loc] => https://test_url//search?tags=Auto%20Repairs
                    [lastmod] => 2017-08-15
                    [changefreq] => daily
                    [priority] => 0.5
                )
        )
)

我的XML文件代码如下所示。

<?xml version="1.0" encoding="utf-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
    <loc>https://test_url/search?tags=Auto%20Repairs</loc>
    <lastmod>2017-08-15</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
    <image:image>
    <image:loc>https://media.test_url/1476130698_business_images.png</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1470780022.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1477691994.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1466467993.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    </url>
    </urlset>

2 个答案:

答案 0 :(得分:1)

您处理特定命名空间中的元素,但由于它们都可以方便地在单个命名空间下使用,因此您可以使用此命名空间(在本例中为前缀)来获取特定节点的所有子节点。因此,->children("image", true);将提取图像命名空间中的所有节点,然后使用foreach()一次浏览每个节点。

$xml = simplexml_load_file($fileName);
$images = $xml->url->children("image", true);
foreach ( $images as $image )   {
    echo $image->loc."=".$image->title.PHP_EOL;
}

这会产生......

https://media.test_url/1470780022.jpg=RV Services Near Me
https://media.test_url/1477691994.jpg=RV Services Near Me
https://media.test_url/1466467993.jpg=RV Services Near Me

答案 1 :(得分:0)

请检查下面的代码工作。这肯定会帮助你继续前进。如果这对你有所帮助,请告诉我。

<?php


$xmlstr = '<?xml version="1.0" encoding="utf-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
    <loc>https://test_url/search?tags=Auto%20Repairs</loc>
    <lastmod>2017-08-15</lastmod>
    <changefreq>daily</changefreq>
    <priority>0.5</priority>
    <image:image>
    <image:loc>https://media.test_url/1476130698_business_images.png</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1470780022.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1477691994.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    <image:image>
    <image:loc>https://media.test_url/1466467993.jpg</image:loc>
    <image:title>RV Services Near Me</image:title>
    </image:image>
    </url>
    </urlset>';
    /* here i am deleting colon */
    $CleanXML = str_replace(["image:image", "image:loc", "image:title", "xmlns:image", "xsi:schemaLocation", "xmlns:xsi"],["imageimage","imageloc","imagetitle", "xmlnsimage", "xsischemaLocation", "xmlnsxsi"], $xmlstr);
    $ParseData = @simplexml_load_string($CleanXML);
    echo "<pre>";
    print_r($ParseData);