Rooneyl
Rooneyl

Reputation: 7902

Error Loading Simple XML

I have an xml document (generated from msword 2010), which I am trying to process using simple xml.

Sample of xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:body>
    <w:p w:rsidR="005B1098" w:rsidRDefault="005B1098"/>
    <w:p w:rsidR="00F254A4" w:rsidRDefault="00F254A4"/>
    <w:p w:rsidR="00F254A4" w:rsidRPr="008475A1" w:rsidRDefault="00C15492" w:rsidP="008475A1">
        <w:pPr>
            <w:jc w:val="center"/>
            <w:rPr>
                <w:b/>
                <w:sz w:val="44"/>
                <w:szCs w:val="44"/>
            </w:rPr>
        </w:pPr>
        <w:r w:rsidRPr="008475A1">
            <w:rPr>
                <w:b/>
                <w:sz w:val="44"/>
                <w:szCs w:val="44"/>
            </w:rPr>
            <w:t>Test file</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="00C15492" w:rsidRPr="008475A1" w:rsidRDefault="00C15492" w:rsidP="008475A1">
        <w:pPr>
            <w:jc w:val="center"/>
            <w:rPr>
                <w:sz w:val="20"/>
                <w:szCs w:val="20"/>
            </w:rPr>
        </w:pPr>
        <w:r w:rsidRPr="008475A1">
            <w:rPr>
                <w:sz w:val="20"/>
                <w:szCs w:val="20"/>
            </w:rPr>
            <w:t>another paragraph</w:t>
        </w:r>
    </w:p>
</w:body>
</w:document>

I am trying to open it using:

$content = '/reports/docx_templates/testing2.xml';
if(!$simple_xml = simplexml_load_file($content)){
    trigger_error('Error reading XML file',E_USER_ERROR);
} else {
    echo 'loaded';
}

And get the error:

Message: simplexml_load_file(): I/O warning : failed to load external entity " Test file another paragraph "

Any ideas?

Upvotes: 1

Views: 1423

Answers (2)

Pete
Pete

Reputation: 1309

I think there's something going on with PHP's XML parser that's not quite right.

http://schemas.openxmlformats.org does not go to a valid web address.... but it doesn't have to because the XML namespace rules don't insist on it. However, look at this page

https://bugs.php.net/bug.php?id=60416

and you see that someone has managed to workaround your problem with a kludge on the namespace.

I'm suspecting that what your original question shows might be successful running of the code but with an error message that you forced by using trigger_error.

This is too long for a comment.

And it's not really an answer, so here's a suggestion to make up for it...

Run this code after saving your xml as testing2.xml in the same directory as the php file.

<?php

if (file_exists('testing2.xml')) {
    $content = file_get_contents('testing2.xml');
    $xml = new SimpleXMLElement($content );
    echo "<pre>";        
    var_dump($xml );
    echo "</pre>"; 
} else {
    exit('Failed to open testing2.xml.');
}

Upvotes: 1

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27295

I prefer the SimpleXMLElement Class.

$xml = new SimpleXMLElement(file_get_contents($content));

I have test it with your XML file and its working.

Edit:

here is a short tutorial to parse a word XML file: Word XML

Upvotes: 1

Related Questions