首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Freemind map xml解析使用simplexml php得到最后的childs

Freemind map xml解析使用simplexml php得到最后的childs
EN

Stack Overflow用户
提问于 2014-05-14 14:52:38
回答 1查看 161关注 0票数 0

在使用SimpleXML解析XML文件时,我遇到了一个问题--实际上freemind映射是什么。

XML示例:

代码语言:javascript
复制
<map version="1.0.1">
<node TEXT="str_1">
    <node TEXT="str_2">
        <node TEXT="str_3"/>
        <node TEXT="str_4">
            <node TEXT="str_5">
                <node TEXT="str_6"/>
            </node>
            <node TEXT="$ str_7"/>
            <node TEXT="str_8"/>
            <node TEXT="$ str_9"/>
        </node>
    </node>
    <node TEXT="str_10"/>
    <node TEXT="str_11"/>
    <node TEXT="$ str_12"/>
</node>
</map>

有了折叠式代码,我就可以得到所有的孩子:

代码语言:javascript
复制
function print_node_info($father, $node)
{

        $output_xml = $node['TEXT'].' - Son of - '.$father.'</br>';

        echo $output_xml;

            // $file = 'output.xml';
            // // Open the file to get existing content
            // $output_xml .= file_get_contents($file);
            // // Write the contents back to the file
            // file_put_contents($file, $output_xml);

                //echo 'father: ' . $father.'<br>';
                //echo 'node: ' . $node['TEXT'].'<br><br>';
                foreach ($node->children() as $childe_node)
                //foreach $xml->xpath("//node[last()]")[0]->attributes() as $Id)
                //foreach ($node as $childe_node) 
                {
                    $GLOBALS['grandfather'] = $father;
                    print_node_info($node['TEXT'], $childe_node);
                }   
}

$xml = simplexml_load_file('1.xml');

foreach ($xml->children() as $first_node) {
print_node_info("top_name", $first_node);
}

我试图得到的只是最后一个孩子的文本值,实际上是不包含子节点的节点。

如能提供任何帮助,将不胜感激。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-14 14:59:41

使用SimpleXMLElement::xpatharray_map可以很容易地做到这一点。

代码语言:javascript
复制
$values = array_map(function($node) {
    return (string) $node['TEXT'];
}, $xml->xpath('//node[not(node)]'));

您可以看到,我们首先得到一个node that do not have children数组,然后将每个节点转换为包含节点的TEXT属性的字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23658154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档