在使用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);
}我试图得到的只是最后一个孩子的文本值,实际上是不包含子节点的节点。
如能提供任何帮助,将不胜感激。
提前感谢!
发布于 2014-05-14 14:59:41
使用SimpleXMLElement::xpath和array_map可以很容易地做到这一点。
$values = array_map(function($node) {
return (string) $node['TEXT'];
}, $xml->xpath('//node[not(node)]'));您可以看到,我们首先得到一个node that do not have children数组,然后将每个节点转换为包含节点的TEXT属性的字符串。
https://stackoverflow.com/questions/23658154
复制相似问题