bcmcfc
bcmcfc

Reputation: 26755

SimpleXML fails without displaying any errors

What could cause the above to occur?

All I'm getting is SimpleXMLElement Object ( ), it's not giving any indication as to why the object is empty. If there's a problem in the XML, it needs to say so.

Upvotes: 0

Views: 362

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

An example of namespaces.

$xmlData = <<<EOXML
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:html="http://www.w3.org/TR/REC-html40">
    <ExcelWorkbook>
        <ss:WindowHeight>13995</ss:WindowHeight>
        <ss:WindowWidth>28455</ss:WindowWidth>
        <ss:WindowTopX>240</ss:WindowTopX>
        <ss:WindowTopY>510</ss:WindowTopY>
        <ss:ProtectStructure>False</ss:ProtectStructure>
        <ss:ProtectWindows>False</ss:ProtectWindows>
    </ExcelWorkbook>
</Workbook>
EOXML;

$xml = new SimpleXMLElement($xmlData);
$namespaces = $xml->getNamespaces(true);

echo 'Show namespaces used';
var_dump($namespaces);

echo 'Read child nodes without using namespace'
$xNodes1 = $xml->ExcelWorkbook->children;

var_dump($xNodes1);

echo 'Read child nodes using namespace'
$xNodes2 = $xml->ExcelWorkbook->children($namespaces['ss']);

var_dump($xNodes2);

Result is:

Show namespaces used

array
  'ss' => string 'urn:schemas-microsoft-com:office:spreadsheet' (length=44)

Read child nodes without using namespace

object(SimpleXMLElement)[4]

Read child nodes using namespace

object(SimpleXMLElement)[5]
  public 'WindowHeight' => string '13995' (length=5)
  public 'WindowWidth' => string '28455' (length=5)
  public 'WindowTopX' => string '240' (length=3)
  public 'WindowTopY' => string '510' (length=3)
  public 'ProtectStructure' => string 'False' (length=5)
  public 'ProtectWindows' => string 'False' (length=5)

Upvotes: 3

Related Questions