我正在使用DOMDocument來讀取XML(SVG),其中有名稱空間。 我遍歷所有節點的屬性,但我似乎無法獲取根元素的'xmlns'類型屬性。DOMDocument xmlns屬性 - 如何獲取它們?
的XML看起來是這樣的:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
id="svg2"
sodipodi:version="0.32"
inkscape:version="0.48.1 "
viewBox="0,0 534,616"
...
>
當我嘗試的東西艾克這樣的:
$node= DOMDocument->documentElement;
foreach($node->attributes as $attribute) {
echo $attribute->nodeName."\n";
}
我得到了 '其他' 屬性,如ID,視框等,但不xlmns :dc等。
有沒有辦法讓他們呢?
請參閱http://stackoverflow.com/questions/2572365/enumerate-registered-namespaces-of-a-php-domdocument-object – 2011-06-09 15:11:48
所以不可能不使用simplexml再讀取整個xml?我需要像讀取它一樣重現XML。我覺得很奇怪,這是不可能的,DOMDocument ... – Dylan 2011-06-09 15:26:07
你可以從一個特定的前綴到其關聯的URI,反之亦然,但是,是的,ext/dom沒有一個函數或屬性獲取您*全部*文檔或節點中使用的名稱空間。 – 2011-06-09 15:29:04