Php 简单xml getName不起作用
我尝试获取xml的元素名。我使用getName()函数。但我不知道为什么它总是出错,它在那一页上出错Php 简单xml getName不起作用,php,xml,simplexml,Php,Xml,Simplexml,我尝试获取xml的元素名。我使用getName()函数。但我不知道为什么它总是出错,它在那一页上出错 $xml=filePath::$xml; //guide to the xml file //use simple xml to get the attributes $xmldoc=simplexml_load_file($xml); //get the children foreach($xmldoc->child
$xml=filePath::$xml; //guide to the xml file
//use simple xml to get the attributes
$xmldoc=simplexml_load_file($xml);
//get the children
foreach($xmldoc->children() as $child) {
foreach($child->attributes() as $a=>$b) {
echo $b;//this statement works correctly
}
echo $child->getName();//this statement does not work, and it leads to the error.
}
为什么?
xml文件如下所示:
根
集合id=“new1”
斜线集合
集合id=“new2”
斜线集合
斜线根
正确的输出应该是:new1 collection new2 collection。但是“集合”无法打印出来。您确定xml文档有效吗 剧本通过了吗
if($xmldoc!=null)
{
它可能声明$xmldoc null,因为该文档无效。请检查以下内容,我给您举了两个示例,说明如何从本地和远程XML加载、获取和打印数据,可能您忘了什么 如果XML文档语法是100%正确的,那么检查它对您也有好处 您可以使用此工具验证XML:
加载本地XML、获取和打印数据:
<?php
// xml example with namespaces
$xml = '<market
xmlns:m="http://mymarket.com/">
<m:fruit>
<m:type>
<m:name from="US">Apples</m:name>
<m:name>Bananas</m:name>
</m:type>
<m:sell>
<m:date>2012-06-24</m:date>
</m:sell>
</m:fruit>
</market>';
// load the xml
$elems = simplexml_load_string($xml);
// evaluate if not null
if($elems != null){
// declare the namespaces
$ns = array(
'm' => "http://mymarket.com/"
);
// for each td inside tr
foreach ($elems->children($ns['m'])->fruit->type->name as $item) {
echo $item->attributes()->from;
echo ',';
echo $item;
}
// get just an element without using loop
echo ','.$elems->children($ns['m'])->fruit->sell->date;
// final output is: US,Apples,Bananas,2012-06-24
}
?>
<?php
$url = "http://www.mymarket.com/products.xml";
// evaluate if not null
if(getXml($url) != null){
// declare the namespaces
$ns = array(
'm' => "http://mymarket.com/"
);
// for each td inside tr
foreach ($elems->children($ns['m'])->fruit->type->name as $item) {
echo $item->attributes()->from;
echo ',';
echo $item;
}
// get just an element without using loop
echo ','.$elems->children($ns['m'])->fruit->sell->date;
// final output is: US,Apples,Bananas,2012-06-24
}
function getXml($url)
{
$xml = @file_get_contents($url);
// If page not found and server has a 404 error redirection, use strpos to look through the $xml
if($xml == false || strpos($xml,'404') == true){
return null;
}
else{
$elems = simplexml_load_string($xml);
return $elems;
}
}
?>
”http://mymarket.com/"
);
//对于tr内的每个td
foreach($elems->children($ns['m'])->fruit->type->name as$item){
echo$item->attributes()->from;
回声',';
echo$项目;
}
//只获取一个元素而不使用循环
echo','.$elems->children($ns['m'])->水果->出售->日期;
//最终产量为:美国,苹果,香蕉,2012-06-24
}
?>
加载远程XML、获取和打印数据:
<?php
// xml example with namespaces
$xml = '<market
xmlns:m="http://mymarket.com/">
<m:fruit>
<m:type>
<m:name from="US">Apples</m:name>
<m:name>Bananas</m:name>
</m:type>
<m:sell>
<m:date>2012-06-24</m:date>
</m:sell>
</m:fruit>
</market>';
// load the xml
$elems = simplexml_load_string($xml);
// evaluate if not null
if($elems != null){
// declare the namespaces
$ns = array(
'm' => "http://mymarket.com/"
);
// for each td inside tr
foreach ($elems->children($ns['m'])->fruit->type->name as $item) {
echo $item->attributes()->from;
echo ',';
echo $item;
}
// get just an element without using loop
echo ','.$elems->children($ns['m'])->fruit->sell->date;
// final output is: US,Apples,Bananas,2012-06-24
}
?>
<?php
$url = "http://www.mymarket.com/products.xml";
// evaluate if not null
if(getXml($url) != null){
// declare the namespaces
$ns = array(
'm' => "http://mymarket.com/"
);
// for each td inside tr
foreach ($elems->children($ns['m'])->fruit->type->name as $item) {
echo $item->attributes()->from;
echo ',';
echo $item;
}
// get just an element without using loop
echo ','.$elems->children($ns['m'])->fruit->sell->date;
// final output is: US,Apples,Bananas,2012-06-24
}
function getXml($url)
{
$xml = @file_get_contents($url);
// If page not found and server has a 404 error redirection, use strpos to look through the $xml
if($xml == false || strpos($xml,'404') == true){
return null;
}
else{
$elems = simplexml_load_string($xml);
return $elems;
}
}
?>
”http://mymarket.com/"
);
//对于tr内的每个td
foreach($elems->children($ns['m'])->fruit->type->name as$item){
echo$item->attributes()->from;
回声',';
echo$项目;
}
//只获取一个元素而不使用循环
echo','.$elems->children($ns['m'])->水果->出售->日期;
//最终产量为:美国,苹果,香蕉,2012-06-24
}
函数getXml($url)
{
$xml=@file\u get\u contents($url);
//如果找不到页面并且服务器有404错误重定向,请使用strpos查看$xml
if($xml==false | | strpos($xml,'404')==true){
返回null;
}
否则{
$elems=simplexml\u load\u字符串($xml);
返回$elems;
}
}
?>
你确定你的意思不是$child->getName()
?我认为看到导致此错误的XML或至少是一个示例会很有用。我认为你忘记了一些东西(可能是你的PHP语法或使用了无效的XML)。我也给你举了两个示例,请查看我的帖子。如果我使用$child->attribute()获取elment的属性,它起作用了。但是当我尝试$child->getName()获取元素名/节点名时,它就不起作用了。为什么?如果我使用$child->attribute()来获取elment的属性,它就可以工作了。但是当我尝试$child->getName()获取元素名/节点名时,它就不起作用了。为什么?试着用我制作的示例来使用XML文档,看看它是否有效(应该有效,我测试过),如果无效,验证XML,看看它是否正常,我还给了你一个工具。我的另一个想法是使用php getname()函数从XML中使用这个简单的示例,如果无效,那么你的XML已损坏,请检查此链接