0

我正在尝试读取我创建的 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>
4

1 回答 1

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>

另外,请意识到您对文件加载的检查在逻辑上是不正确的。当相反的情况发生时,您正在输出文件已成功加载。

于 2013-08-31T18:55:02.223 回答