How to parse xml namespaces with DOMDocument and be sure to get objects

泄露秘密 提交于 2019-12-02 09:43:43

You need to use DOMDocument::getElementsByTagNameNS() which is for use with namespaced elements. Find the namespace that domain points to and pass it to the method along with the target tag name e.g. roid or name.

Codepad Demo

$dom = new DOMDocument();
$dom->loadXML($xml);

$ns = 'http://epp.nic.ir/ns/domain-1.0';

$data = $dom->getElementsByTagNameNS($ns, 'infData')->item(0);

$roid = $data->getElementsByTagNameNS($ns, 'roid')->item(0)->nodeValue;
$name = $data->getElementsByTagNameNS($ns, 'name')->item(0)->nodeValue;
// repeat the same for others

echo $roid . "\n";
echo $name;

Outputs

305567
pooyaos.ir

Simply put in your url into simple_load_file()

<?php
$xml = simplexml_load_file($xmlfile);
print $xml->infData;   // Sample content
?> 

This is a easiest method

But infData is not your tagName. This XML uses namespaces (domain is your namespace). Don't have experience with that so I really can help you any futher then the hint where to look. The getElementsByTagName is not going to work, you need something like DOMXPath I think that's where you can do something with namespaces.

Sorry I can't be of futher help but since no one mentioned the namespaces I thought I would just add the answer anyway. Maybe it will help you Google for an answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!