【发布时间】:2014-06-06 07:26:45
【问题描述】:
我尝试从 xml 文件中获取数据,但无法获取数据, 例如,如何获取 caaml:locRef 值或 caaml:beginPosition 值?
这是目前为止的代码:
/* a big thank you to helderdarocha */
/* – he already helped me yesterday with a part of this code */
$doc = new DOMDocument();
$doc->load('xml/test.xml');
$xpath = new DOMXpath($doc);
$xpath->registerNamespace("caaml", "http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS");
if ($doc->schemaValidate('http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS/CAAMLv5_BulletinEAWS.xsd')) {
foreach ($xpath->query('//caaml:DangerRating') as $key) {
echo $key->nodeValue;
print_r($key);
}
}
这里是 $key 的 print_r
DOMElement Object ( [tagName] => caaml:DangerRating [schemaTypeInfo] => [nodeName] => caaml:DangerRating [nodeValue] => 2014-03-03+01:00 2 [nodeType] => 1 [parentNode] => (object value omitted) [childNodes] => (object value omitted) [firstChild] => (object value omitted) [lastChild] => (object value omitted) [previousSibling] => (object value omitted) [nextSibling] => (object value omitted) [attributes] => (object value omitted) [ownerDocument] => (object value omitted) [namespaceURI] => http://caaml.org/Schemas/V5.0/Profiles/BulletinEAWS [prefix] => caaml [localName] => DangerRating [baseURI] => /Applications/MAMP/htdocs/lola/xml/test.xml [textContent] => 2014-03-03+01:00 2 ) 2014-03-04+01:00 2
这里是xml的一部分
<caaml:DangerRating>
<caaml:locRef xlink:href="AT7R1"/>
<caaml:validTime>
<caaml:TimePeriod>
<caaml:beginPosition>2014-03-06T00:00:00+01:00</caaml:beginPosition>
<caaml:endPosition>2014-03-06T11:59:59+01:00</caaml:endPosition>
</caaml:TimePeriod>
</caaml:validTime>
<caaml:validElevation>
<caaml:ElevationRange uom="m">
<caaml:beginPosition>2200</caaml:beginPosition>
</caaml:ElevationRange>
</caaml:validElevation>
<caaml:mainValue>2</caaml:mainValue>
</caaml:DangerRating>
<caaml:DangerRating>
<caaml:locRef xlink:href="AT7R1"/>
<caaml:validTime>
<caaml:TimePeriod>
谢谢!
【问题讨论】: