Araw :
Is there a way to get the namespaces in a XML document using XMLReader?
For now I am using SimpleXMLElement::getDocNamespaces()
to do the job, but since I am working with rather big documents there is a drawback since (as far as I know) SimpleXML loads the whole document at once with respect to memory usage.
Thanks for any help!
Nigel Ren :
Based on the code from https://stackoverflow.com/a/12652385/1213708 which shows how to read the attributes of a document using XMLReader, this just checks if the attribute has a prefix of xmlns
and if it does, it adds it into a list of namespaces using prefix(which is the localName
value)/URI (which is the attribute value)...
$xml = new XMLReader;
$xml->open($fileName);
$doc = new DOMDocument;
$namespaces = [];
while ($xml->read()) {
if($xml->hasAttributes) {
while($xml->moveToNextAttribute()) {
if ( $xml->prefix == 'xmlns' ) {
$namespaces [ $xml->localName ] = $xml->value;
}
}
}
}
print_r($namespaces);
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=346664&siteId=1