用php迭代XML文件

用php迭代XML文件,php,xml,Php,Xml,我有一个包含以下内容的XML文件: <property> <id>1</id> <type>type</type> <town>town</town> <province>province</province> <images> <image id="1"> <url> http://www.test.co

我有一个包含以下内容的XML文件:

<property>
  <id>1</id>
  <type>type</type>
  <town>town</town>
  <province>province</province>
  <images>
    <image id="1">
    <url>
      http://www.test.com
    </url>
    <image id="2">
    <url>
      http://www.test.com
    </url>
    <image id="3">
    <url>
      http://www.test.com
    </url>
  </image>
我可以遍历该文件并获取除图像url之外的值。 我正在处理一个属性元素之后的元素

$count=0;
$id=0;
foreach($xml->children() as $properties) {
    echo "<h1>" . $xml->property[$count]->type . " for sale in " .$xml->property[$count]->town . ", " . $xml->property[$count]->province . "</h1>" . "<br>";
    echo $xml->property[$count]->id . "<br>";
    echo $xml->property[$count]->desc->en . "<br>";

    foreach($xml->property[$count]->children() as $images) {
        echo $xml->property[$count]->images -> image[$id++] -> url;
        $id++;
}
    $count++;
}
但第二个循环并不接近正确。
我将非常感谢您的帮助。

像这样的事情也许:

foreach($xml->property[$count]->images as $image) {
    echo $image->url;
}

您尚未共享完整的XML结构,因此不清楚文档的根在哪里是文档元素

假设根元素不是,但所有元素都是根元素的子元素,则只需迭代即可迭代所有这些元素:

$xml = simplexml_load_file('example.xml');

foreach ($xml->property as $property) {
    printf(
        "<h1>%s for sale in %s, %s</h1>\n", htmlspecialchars($property->id), 
        htmlspecialchars($property->town), htmlspecialchars($property->province)
    );
如果在这里不使用xpath,则需要为包含多个子级的每个级别创建foreach。作为反例:

foreach ($property->images->image as $image) {
    foreach ($image->url as $url) {
        printf(" - %s\n", htmlspecialchars(trim($url)));
    }
}
我希望这能更好地向您展示在simplexml中遍历是如何工作的。PHP手册中有一些更重要的内容,标题为“显示基本遍历,并链接到更高级的xpath主题”

不要忘记将动态数据输出到HTML中以正确地进行HTML编码。我已经使用了这个函数,您需要提供正确的编码参数,为了简洁起见,我在回答中省略了这些参数

完整示例:

<?php
/**
 * @link https://stackoverflow.com/questions/28929239/iterating-through-xml-file-with-php
 */

$buffer = <<<XML
<root>
    <property>
        <id>1</id>
        <type>type</type>
        <town>town</town>
        <province>province</province>
        <images>
            <image id="1">
                <url>
                    http://www.test.com
                </url>
            </image>
            <image id="2">
                <url>
                    http://www.test.com
                </url>
            </image>
            <image id="3">
                <url>
                    http://www.test.com
                </url>
            </image>
        </images>
    </property>
    <property>
        <id>1</id>
        <images>
            <image id="1">
                <url>
                    http://www.test.com
                </url>
            </image>
         </images>
    </property>
</root>
XML;


$xml = simplexml_load_string($buffer);

foreach ($xml->property as $property) {
    printf(
        "<h1>%s for sale in %s, %s</h1>\n",
        htmlspecialchars($property->id), htmlspecialchars($property->town), htmlspecialchars($property->province)
    );

    $urls = $property->xpath('images/image/url');
    foreach ($urls as $url) {
        printf(" - %s\n", htmlspecialchars(trim($url)));
    }
}
要从字符串操作中获益,您只需使用这个类名XmlTranslated,或者在创建SimpleXMLElement时命名该类:

-或者-这对于simplexml来说非常特殊,稍后您可以使用一些转换技巧来更改该类:

$xml = simplexml_import_dom(dom_import_simplexml($xml), 'XMLTransliterated');
这将使以下代码使用XmlTranslaterated而不是以前不太具体的SimpleXMLElement,因此代码大体保持不变。请注意,可以安全地删除htmlspecialchars和trim调用,因为现在在访问SimpleXMLElement的字符串值时会自动调用它们:

更多相关音译问题:

2008年10月 2010年8月
你想要的最终结果是什么?对不起,这会有帮助的!我想显示每个图像。我只需要返回for images->image[id..]->url值的代码。我不知道正确的xml术语,但每个元素都包含一个带有子元素的元素,并将其作为一个元素。我想要每个图像的url。http://http://http://many 谢谢,这很有效。我试图添加评论,但无法添加。我也试着把它标记为正确的。编码部分对我来说很难。你知道我如何改变像o和a这样的特殊字符吗?如果不知道,至少要正确显示它们$输出=用于销售的财产$财产->城镇$属性->省..$count..html;我尝试了各种方法,包括标题,但没有成功。我已经成功地使用charset=ISO-8859-1将字符显示为,但无法将字符格式化为o。@sharon:您创建的输出的字符编码是什么?再次感谢您的帮助hakre。为了给遇到它的其他人完成线程,我决定使用utf-8,然后使用替换函数$prop_province=removeAccents$property->province格式化;我在这里找到的。我找不到任何其他的故障保护方法。
<h1>1 for sale in town, province</h1>
 - http://www.test.com
 - http://www.test.com
 - http://www.test.com
<h1>1 for sale in , </h1>
 - http://www.test.com
/**
 * Transliterate and HTML encode
 *
 * Class XMLTransliterated
 */
class XMLTransliterated extends SimpleXMLElement
{
    public static $transliterator;

    public function __toString()
    {
        $transliterator = &self::$transliterator;
        $transliterator || $transliterator = Transliterator::create("Latin-ASCII");

        $transliterated = $transliterator->transliterate($this);

        return htmlspecialchars(trim($transliterated), ENT_QUOTES | ENT_HTML5, 'UTF-8');
    }
}
$xml = simplexml_load_string($buffer, 'XMLTransliterated');
$xml = simplexml_import_dom(dom_import_simplexml($xml), 'XMLTransliterated');
$xml = simplexml_load_string($buffer, 'XMLTransliterated');

foreach ($xml->property as $property) {
    printf(
        "<h1>%s (%s) for sale in %s, %s</h1>\n",
        $property->id, $property->type, $property->town, $property->province
    );

    $urls = $property->xpath('images/image/url');
    foreach ($urls as $url) {
        printf(" - %s\n", $url);
    }
}
$transliterated = iconv('UTF-8', 'ASCII//IGNORE//TRANSLIT', $this);