Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php显示XML标记完整路径_Php_Xml - Fatal编程技术网

用php显示XML标记完整路径

用php显示XML标记完整路径,php,xml,Php,Xml,假设我们要处理此提要: 我试图以这种方式显示XML文件的节点: deals->deal->dealsite deals->deal->deal_id deals->deal->deal_title 这是为了能够处理我们不知道其XML标记是什么的提要。因此,我们将让用户选择deals->deal->deal\u title是交易标题,并以这种方式识别它 我多年来一直在尝试使用以下代码: class HandleXML { var $root_t

假设我们要处理此提要:

我试图以这种方式显示XML文件的节点:

deals->deal->dealsite
deals->deal->deal_id
deals->deal->deal_title
这是为了能够处理我们不知道其XML标记是什么的提要。因此,我们将让用户选择deals->deal->deal\u title是交易标题,并以这种方式识别它

我多年来一直在尝试使用以下代码:

    class HandleXML {
    var $root_tag = false;
    var $xml_tags = array();
    var $keys = array();

function parse_recursive(SimpleXMLElement $element)
{
        $get_name = $element->getName();
        $children   = $element->children();     // get all children

        if (empty($this->root_tag)) {
            $this->root_tag = $this->root_tag.$get_name;
        }

        $this->xml_tags[] = $get_name;

        // only show children if there are any
        if(count($children))
        {
               foreach($children as $child)
               {
                $this->parse_recursive($child); // recursion :)
               }
        }
        else {
            $key = implode('->', $this->xml_tags);
            $this->xml_tags = array();
            if (!in_array($key, $this->keys)) {
                if (!strstr('>', $key) && count($this->keys) > 0) { $key = $this->root_tag.'->'.$key; }
                if (!in_array($key, $this->keys)) {
                    $this->keys[] = $key;
                }
            }
        }
    }
}

$xml = new SimpleXMLElement($feed_url, null, true);
$handle_xml = new HandleXML;

$handle_xml->parse_recursive($xml);
foreach($handle_xml->keys as $key) {
    echo $key.'<br />';
}
exit;
见第2行和第3行,交易->部分缺失

我也尝试过这段代码:但这肯定不是最好的方法,而且也不总是有效的


无论有多少次我都做不到。

在我的一个站点中,我使用了一种稍微不同的方法来处理xml提要。在您的情况下,它看起来像:

$xml = simplexml_load_file("http://tools.forestview.eu/xmlp/xml_feed.php?aid=1094&cid=1000");

foreach($xml->{'deal'} as $deal) 
{
$dealsite = $deal->{'dealsite'};
$dael_id = $deal->{'dael_id'};
$deal_title = $deal->{'deal_title'};
$deal_url = $deal->{'deal_url'};
$deal_city = $deal->{'deal_city'};
$deal_category = $deal->{'deal_category'};

// and so on for the rest

// do some stuff with the variables like insert into MySQL

}

问题是,在我的例子中,我不知道XML标记,这个级别是对标记的识别。用户必须告诉脚本每个标记的用途。
$xml = simplexml_load_file("http://tools.forestview.eu/xmlp/xml_feed.php?aid=1094&cid=1000");

foreach($xml->{'deal'} as $deal) 
{
$dealsite = $deal->{'dealsite'};
$dael_id = $deal->{'dael_id'};
$deal_title = $deal->{'deal_title'};
$deal_url = $deal->{'deal_url'};
$deal_city = $deal->{'deal_city'};
$deal_category = $deal->{'deal_category'};

// and so on for the rest

// do some stuff with the variables like insert into MySQL

}