0

SimpleXML を使用して XML ファイルを解析するときに、実際に freemind がマップするものに問題があります。

XML の例:

<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>

そして、次のコードを使用すると、すべての子を取得できます。

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);
}

私が取得しようとしているのは、最後のすべての子の TEXT 値、実際には子を含まないノードのみです。

どんな助けでもいただければ幸いです

前もって感謝します!

4

1 に答える 1

0

SimpleXMLElement::xpathと を使用すると、これをかなり簡単に行うことができますarray_map

$values = array_map(function($node) {
    return (string) $node['TEXT'];
}, $xml->xpath('//node[not(node)]'));

最初にchildren を持たないノードの配列を取得し、次にこれらの各ノードをノードのTEXT属性を含む文字列に変換することがわかります。

于 2014-05-14T14:59:41.940 に答える