SimpleXML失败而不显示任何错误

时间:2011-03-17 11:46:45

标签: php xml simplexml

可能导致上述情况发生的原因是什么?

我得到的只是SimpleXMLElement Object ( ),它没有说明为什么对象是空的。如果XML中存在问题,则需要这样说。

1 个答案:

答案 0 :(得分:3)

命名空间的一个例子。

$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);

结果是:

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)