Php 使用特殊名称空间解析XML
我得到了一个如下所示的XML文件:Php 使用特殊名称空间解析XML,php,xml,dom,Php,Xml,Dom,我得到了一个如下所示的XML文件: <?XML version="1.0" encoding="UTF-8"?> <test:start xmlns:test="http://www.test.de" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <test:uebertragung art="OFFLINE" version="1.0.0"></test:ueber
<?XML version="1.0" encoding="UTF-8"?>
<test:start xmlns:test="http://www.test.de" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<test:uebertragung art="OFFLINE" version="1.0.0"></test:uebertragung>
<test:object>
<test:objectnr>5</test:objectnr>
<test:objectview>
<test:geo>
<test:lat>12</test:lat>
<test:long>30</test:long>
</test:geo>
<test:objectcategorie>5</test:objectcategorie>
<test:geo>
<test:lat>11</test:lat>
<test:long>30</test:long>
</test:geo>
<test:objectcategorie>8</test:objectcategorie>
<test:geo>
<test:lat>16</test:lat>
<test:long>30</test:long>
</test:geo>
<test:objectcategorie>2</test:objectcategorie>
<test:geo>
<test:lat>14</test:lat>
<test:long>35</test:long>
</test:geo>
<test:objectcategorie>14</test:objectcategorie>
</test:objectview>
</test:object>
</test:start>
5.
12
30
5.
11
30
8.
16
30
2.
14
35
14
现在我想用php解析这个文件。我得到以下代码,它只显示第一个对象:
$xmlDoc->load("test.xml");
$x = $xmlDoc->documentElement;
$x = $x->childNodes[1];
$x = $x->childNodes[1];
foreach ($x->childNodes AS $item) {
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
$xmlDoc->load(“test.xml”);
$x=$xmlDoc->documentElement;
$x=$x->childNodes[1];
$x=$x->childNodes[1];
foreach($x->childNodes作为$item){
打印$item->nodeName。“=”$item->nodeValue。“
”;
}
谁能给我解释一下解析这个XML文件的简单方法吗。我只想显示所有“objectsviews”和“objectcategorie”中的“test:lat” 希望这将有助于您解决问题
libxml_use_internal_errors( TRUE );
$dom = new DOMDocument('1.0','utf-8');
$dom->validateOnParse=false;
$dom->standalone=true;
$dom->strictErrorChecking=false;
$dom->recover=true;
$dom->load( 'test.xml' );
libxml_clear_errors();
$xpath=new DOMXPath( $dom );
$rns = $dom->lookupNamespaceUri( $dom->namespaceURI );
$xpath->registerNamespace( 'test', $rns );
$col = $xpath->query('//test:geo');
foreach( $col as $node ) {
foreach( $node->childNodes as $cn ){
if( is_object( $cn ) && $cn->nodeType==1 ) echo $cn->tagName.'='.$cn->nodeValue.'<br />';
}
}
$dom=$xpath=$rns=$col=null;
/*
This outputs:
-------------
test:lat=12
test:long=30
test:lat=11
test:long=30
test:lat=16
test:long=30
test:lat=14
test:long=35
*/
libxml\u使用\u内部错误(TRUE);
$dom=新的DOMDocument('1.0','utf-8');
$dom->validateOnParse=false;
$dom->standalone=true;
$dom->strigerrorchecking=false;
$dom->recover=true;
$dom->load('test.xml');
libxml_clear_errors();
$xpath=newdomxpath($dom);
$rns=$dom->lookupNamespaceUri($dom->namespaceURI);
$xpath->registerNamespace('test',$rns);
$col=$xpath->query('//test:geo');
foreach($col作为$node){
foreach($node->childNodes作为$cn){
如果(is_object($cn)&&$cn->nodeType==1)回显$cn->tagName'='.$cn->nodeValue'.
;
}
}
$dom=$xpath=$rns=$col=null;
/*
这将产生:
-------------
测试:lat=12
测试:长=30
测试:lat=11
测试:长=30
测试:lat=16
测试:长=30
测试:lat=14
测试:长=35
*/
请使用搜索或阅读DOM手册