simpleXML在不工作的PHP 5.4

时间:2014-03-12 20:19:34

标签: php simplexml

在我的开发系统上Php 5.3以下SimpleXML对象正常工作:

    $fs = "lectio1"
    $xmldoc = simplexml_load_file($filename);
    $ln = $xmldoc->xpath("//item[fileName/@filestem='$fs']/title");
    print_r($ln)


Array ( [0] => SimpleXMLElement Object ( [0] => Lectio 1, Prologus ) )

但是php 5.4中的相同功能会产生以下结果。

Array ( [0] => SimpleXMLElement Object ( ) )

任何人都可以帮我弄清楚发生了什么吗?

XML文件的片段如下:

 <?xml version="1.0" encoding="UTF-8"?>
 <listofFileNames>
 <div id="body">
    <div id="principia" class="toplevel">
        <head>Principia</head>
        <item>
            <fileName filestem="principiumI">principiumI.xml</fileName>
            <title>Principium I</title>
            <hasParts>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="principiumII">principiumII.xml</fileName>
            <title>Principium II</title>
            <hasParts>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="principiumIII">principiumIII.xml</fileName>
            <title>Principium III</title>
            <hasParts>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="principiumIV">principiumIV.xml</fileName>
            <title>Principium IV</title>
            <hasParts>
                <part>
                    <slug>reims</slug>
                    <title>Reims</title>
                    <initial>R</initial>
                </part>
                <part>
                    <slug>svict</slug>
                    <title>St. Victor</title>
                    <initial>SV</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
    </div>
    <div id="deFide" class="toplevel">
        <head>de Fide</head>
        <item>
            <fileName filestem="lectio1">lectio1.xml</fileName>
            <title>Lectio 1, Prologus</title>
            <hasParts>
                <part>
                    <slug>reims</slug>
                    <title>Reims</title>
                    <initial>R</initial>
                </part>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
                <part>
                    <slug>svict</slug>
                    <title>St. Victor</title>
                    <initial>SV</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="lectio2">lectio2.xml</fileName>
            <title>Lectio 2, Prologus</title>
            <hasParts>
                <part>
                    <slug>reims</slug>
                    <title>Reims</title>
                    <initial>R</initial>
                </part>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
                <part>
                    <slug>svict</slug>
                    <title>St. Victor</title>
                    <initial>SV</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
        <item>
            <fileName filestem="lectio3">lectio3.xml</fileName>
            <title>Lectio 3, Prologus</title>
            <hasParts>
                <part>
                    <slug>reims</slug>
                    <title>Reims</title>
                    <initial>R</initial>
                </part>
                <part>
                    <slug>vat</slug>
                    <title>Vatican</title>
                    <initial>V</initial>
                </part>
                <part>
                    <slug>svict</slug>
                    <title>St. Victor</title>
                    <initial>SV</initial>
                </part>
                <part>
                    <slug>sorb</slug>
                    <title>Sorbonne</title>
                    <initial>S</initial>
                </part>
            </hasParts>
        </item>
     </div>
   </div>
 </listofFileNames>

1 个答案:

答案 0 :(得分:0)

在谈到print_r时,不要依赖var_dumpSimpleXml,而应该:

foreach ($ln as $l)
    echo htmlentities($l->asXML());

使用此方法,5.3.10与5.4.9 的结果相同

<title>Lectio 1, Prologus</title>

PHP 5.3.10:见http://codepad.viper-7.com/RL8L0i

PHP 5.4.9:见http://codepad.viper-7.com/0JgAmQ