0

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.

1
  • 1
    Check the use of namespaces within your XML. This is not necessarily a problem in the XML, but you could be trying to read a namespaced node without using the appropriate namespace... which will give you an empty object. Show us the XML object that you're trying to read Commented Mar 17, 2011 at 11:50

1 Answer 1

3

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)
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.