0

I'm trying to read an XML file that I created. I'm using this code, but getting nothing out except 'the file was loaded successfully'. I've tried xpath and many others, but it just doesn't output. It is possible there is something wrong with my XML because I created the file by hand and I've never made an XML file before.

Ultimately, my goal is to be able to read an extension attribute and get the description, class, and icon.

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

Here is the 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>
3
  • 1
    Your XML is invalid - it does not have one root element. And since <group> contains other element it should not be self-enclosing (/> at the end of opening tag) Commented Aug 31, 2013 at 18:46
  • 1
    Your logic is backward - you have output the message that the file loaded successfully when !$xml, which means it actually failed, owing the the invalid XML mentioned previously. Commented Aug 31, 2013 at 18:47
  • Check libxml_get_errors() to see what SimpleXML is complaining about. Commented Aug 31, 2013 at 18:48

1 Answer 1

1

Your XML looks invalid. You need a root element that wraps all the rest inside it. Also, you should not close elements that contain other elements right in the opening tag, such as in:

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

That should instead be written as:

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

Basically your XML should look like this:

<?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>

Also, realize that your check for the file loading is logically incorrect. You are outputting that the file was loaded successfully when the contrary happens.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.