如何使用命名空间获取属性

时间:2013-06-21 14:26:06

标签: php xml domdocument xml-namespaces

如何使用PHP的DOMDocument(以及必要的DOMXPath)获取xsi:nil属性值?

<?xml version="1.0"?>
<Rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Row Index="1">
    <Email xsi:nil="true"/>
  </Row>
</Rows>

我尝试了getAttribute / getAttributeNS / getAttributeNodeNS,但没有成功。

提前致谢

1 个答案:

答案 0 :(得分:2)

试试这段代码,我刚刚测试过它。

<?php
$xml=<<<EOF
<?xml version="1.0"?>
<Rows xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Row Index="1">
    <Email xsi:nil="true"/>
  </Row>
</Rows>
EOF;

$doc = new DOMDocument();
$doc->loadXML($xml);
$emails = $doc->getElementsByTagName('Email');
var_dump($emails->item(0)->attributes->getNamedItem('nil'));
相关问题