2014-05-14 1 views
0

XML 파일 SimpleXML로 구문 분석 할 때 실제로 freemind 맵과 관련된 문제가 있습니다.Freemind지도 xml 구문 분석 Simplexml PHP를 사용하여 마지막 자식 가져 오기

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> 

그리고 코드를 folowing로 난 모든 차일 얻을 수 있습니다 :

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 값, 실제로 노드입니다 어린이.

어떤 도움을 사전에

감사 감사하겠습니다!

답변

0

SimpleXMLElement::xpatharray_map을 사용하면 쉽게 할 수 있습니다. 먼저 우리가 node that do not have children의 배열을 얻을 수 있음을 알 수

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

는, 우리는 노드의 TEXT 속성을 포함하는 문자열에 해당 노드의 각 변환.

+0

감사합니다. Tim, var_dump와 함께 작동하는 것을 알 수 있지만, 저의 경우 기본 레벨에 가까운 PHP를 사용하기가 쉽지 않습니다. – vikprostokvasha

관련 문제