PHP SimpleXML不输出任何内容

时间:2013-08-31 18:45:06

标签: php xml simplexml

我正在尝试读取我创建的XML文件。我正在使用此代码,但除了“文件已成功加载”之外什么都没有。我尝试过xpath和其他许多,但它只是没有输出。我的XML可能有问题,因为我手工创建了文件,而且我以前从未创建过XML文件。

最终,我的目标是能够阅读扩展属性并获取描述,类和图标。

<?php
if(!$xml = simplexml_load_file('formats.xml')) {
    echo 'the file was loaded successfully';
    print_r($xml);
}
else {
    echo 'the file was not loaded';
}
?>

这是XML:

<?xml version="1.0"?>
<group class="doc" icon="./.images/doc.png" />
    <type extension="doc" description="Legacy Word Document" />
    <type extension="docx" description="XML Word Document" />
    <type extension="ppt" description="Legacy PowerPoint" />
    <type extension="pptx" description="XML PowerPoint" />
    <type extension="pps" description="Legacy PowerPoint Show" />
    <type extension="ppsx" description="XML PowerPoint Show" />
    <type extension="docm" description="Macro Enabled Word Document" />
    <type extension="dot" description="Legacy Word Template" />
    <type extension="dotx" description="XML Word Template" />
    <type extension="dotm" description="Macro Enabled Word Template" />
    <type extension="log" description="A Log File" />
    <type extension="msg" description="A Message File" />
    <type extension="odt" description="OpenOffice Document" />
    <type extension="pages" description="Apple Pages Document" />
    <type extension="rtf" description="Rich Text Format" />
    <type extension="tex" description="LaTeX Document" />
    <type extension="wpd" description="Word Perfect Document" />
    <type extension="ini" description="Settings File" />
    <type extension="cfg" description="Config File" />
    <type extension="conf" description="Config File" />
    <type extension="nfo" description="Information File" />
    <type extension="inf" description="Autorun File" />
    <type extension="wps" description="Works Document" />
</group>

<group class="video" icon="./.images/video.png" />
    <type extension="asf" description="Advanced Streaming Format" />
    <type extension="asx" description="Windows Media Playlist" />
    <type extension="avi" description="Audio-Video Interleave" />
    <type extension="flv" description="Flash Player Video" />
    <type extension="mkv" description="Matroska Video" />
    <type extension="mov" description="Apple Movie" />
    <type extension="mp4" description="MPEG 4 Video" />
    <type extension="mpg" description="MPEG Video" />
    <type extension="rm" description="RealMedia Video" />
    <type extension="srt" description="Subtitles File" />
    <type extension="swf" description="Flash Animation" />
    <type extension="vob" description="DVD Video File" />
    <type extension="wmv" description="Windows Media Video" />
    <type extension="mpeg" description="Motion Picture Experts Video" />
</group>

1 个答案:

答案 0 :(得分:1)

您的XML看起来无效。你需要一个根元素来包装其中的所有其余元素。此外,您不应该在开始标记中关闭包含其他元素的元素,例如:

<group class="doc" icon="./.images/doc.png" />

应该写成:

<group class="doc" icon="./.images/doc.png">

基本上你的XML应该是这样的:

<?xml version="1.0"?>
<extensions>
    <group class="doc" icon="./.images/doc.png">
        <type extension="doc" description="Legacy Word Document" />

        ...

        <type extension="wps" description="Works Document" />
    </group>

    <group class="video" icon="./.images/video.png">
        <type extension="asf" description="Advanced Streaming Format" />

        ...

        <type extension="mpeg" description="Motion Picture Experts Video" />
    </group>
</extensions>

另外,请注意,检查文件加载在逻辑上是不正确的。当输出相反时,您正在输出文件已成功加载。