Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
indent_list_to_xml.php
1 <?php
46 function indent_list_to_array(&$lines)
47 {
48  $res = Array();
49  $indent_size = get_indent_size(current($lines));
50  while (true) {
51  $current_line = current($lines);
52  //echo "$current_line \n";
53  $line_content = trim($current_line);
54  if (get_indent_size($current_line) < $indent_size) {
55  break;
56  }
57  if (false === next($lines)) break;
58  if (empty($line_content)) continue;
59  if (get_indent_size(current($lines)) > $indent_size) {
60  $res[$current_line] = indent_list_to_array($lines);
61  } else {
62  $res[$current_line] = Array();
63  }
64  }
65  return $res;
66 
67 }//end indent_list_to_array()
68 
69 
80 function get_indent_size($s)
81 {
82  $res = 0;
83  while ($s{$res} == "\t") {
84  $res++;
85  }
86  return $res;
87 
88 }//end get_indent_size()
89 
90 
100 function print_array_xml_r($res, $indent='')
101 {
102  if (empty($res)) {
103  echo '(empty)';
104  } else {
105  foreach ($res as $item => $kids) {
106  echo $indent.'<term name="'.htmlspecialchars(trim($item)).'">';
107  if (!empty($kids)) {
108  echo "\n";
109  echo $indent."\t".'<relation name="Category">'."\n";
110  print_array_xml_r($kids, $indent."\t\t");
111  echo $indent."\t".'</relation>'."\n";
112  echo $indent;
113  }
114  echo '</term>'."\n";
115  }
116  }
117 
118 }//end print_array_xml_r()
119 
120 
121 // MAIN //
122 if ($argc != 2) {
123  echo "Usage: php indent_list_to_xml inputfile.txt > outputfile.xml \n";
124  exit();
125 }
126 ?>
127 <thesaurus>
128 <?php
129  $lines = file($argv[1]);
130  reset($lines);
131  $res = indent_list_to_array($lines, $res);
132  print_array_xml_r($res, "\t\t");
133 ?>
134 </thesaurus>