Given the input array of values ​​as a string:

(p (b "sometext")(p (table (columns (column "One") (column "Two") (column "Three")) (body (row (cell "111.1") (cell "111.2") (cell "111.3") nil nil) (row (cell "222.1") (cell "222.2") (cell "222.3") nil nil) (row (cell "333.1") (cell "333.2") (cell "333.3") nil nil))))) 

It's all. It is necessary to parse in XML or HTML using javascript or php. I can not find an algorithm that will help me build a tree from the list and work on this tree in the future. The given line for parsing is a fragment, in fact there is more data, but their structure in general is as follows. Tell me with the algorithm or give a link to the implementation, what I found - I can not stick to my task. It is advisable not to use complex components such as treeview, but to do with arrays or lists.

  • in principle everything is real, but it’s not clear what to do with nil - KoVadim
  • show a real example !!! I google for three days, they write “all this is simple” on all resources, but no one shows the code - deivan_
  • it is simply written in lisp style. I can write a parser, so I ask how the parser should interpret nil. - KoVadim
  • let's say like space ( & nbsp; ) - deivan_

1 answer 1

Just a couple of seconds googling and is the site . There is a list2xml converter, though it is on python. Finished a little bit by the file and ready!

upd: And here is the code in php. I know PHP very badly, therefore, the code will need to be finished to the desired state. By generating a piece of lisp code, it generates a valid xml.

 <?php class Lisp2XML_FST { public $_prev; public $_tags; public function __construct($root="root", $empty="empty", $readfrom='') { $this->emptytag = $empty; $this->roottag = $root; $this->readfrom = $readfrom; $this->_tags = array(); $this->_prev = ''; } public function is_startparens() { $this->is_tag(); array_push($this->_tags, ""); } public function is_tag() { $l = count($this->_tags); if ($this->_tags[$l -1] != "") { $prevtag = $this->_tags[$l -1]; } else { $prevtag = $this->emptytag; } $this->_tags[$l - 1] = $prevtag; $this->write("<".$prevtag.">"); } public function is_endparens() { $tag = array_pop($this->_tags); $this->write("</".$tag.">\n"); } public function write($something) { echo($something); #весь вывод - только через эту функцию } public function comment($char) { if (char == "\n") { return $this->_prev; } else { return "comment"; } } public function starttag($char) { $this->_prev = "starttag"; if (($char == " ") or ($char == "\n") or ($char == "\t")) { return "starttag"; } else if ($char == "(") { array_push($this->_tags, ""); return "intag"; } else if ($char == "%") { return "comment"; } else { return "error"; } } public function intag($char) { $this->_prev = "intag"; if ($char == "(") { $this->is_startparens(); return "intag"; } elseif (($char == " ") or ($char == "\n") or ($char == "\t")) { $l = count($this->_tags); if ($this->_tags[$l-1] == "") { echo("tag\n"); return "intag"; } else { $this->is_tag(); return "indata"; } } else if ($char == "%") { return "comment"; } else if ($char == ")") { $this->is_endparens(); return "indata"; } else { $l = count($this->_tags); $this->_tags[$l-1] .= $char; return "intag"; } } public function indata($char) { $this->_prev = "indata"; $default = "indata"; if ($char == "(") { array_push($this->_tags, ""); return "intag"; } else if ($char == ")") { $this->is_endparens(); return $default; } else if (($char == " ") or ($char == "\n") or ($char == "\t")) { $this->write($char); return $default; } else if ($char == "%") { return "comment"; } else { $this->write($char); return $default; } } public function error($char) { return ""; } }; function convert($root="root", $empty="empty", $readfrom="") { $fst = new Lisp2XML_FST($root, $empty, $readfrom); $fst->write("<".$fst->roottag.">"); for ($i = 0; $i < strlen($readfrom); $i++) { $char = $readfrom[$i]; if (count($fst->_tags) == 0) { $next = "starttag"; $fst->write("\n"); } $prev = $next; $next = $fst->$next($char); if ($next == "") { if (count($fst->_tags) != 0) { $fst->is_endparens(); } break; } } if (count($fst->_tags)) { $fst->is_endparens(); } $fst->write("</".$fst->roottag.">\n"); } ?> <?php #пример использования $s = '(p (b "sometext")(p (table (columns (column "One") (column "Two") (column "Three"))'. '(body (row (cell "111.1") (cell "111.2") (cell "111.3") nil nil)'. '(row (cell "222.1") (cell "222.2") (cell "222.3") nil nil)'. '(row (cell "333.1") (cell "333.2") (cell "333.3") nil nil)))))'; convert('root', 'empty', $s); ?> 
  • Thank you, but my file does not understand the python ... - deivan_
  • Python is not complicated. If I can rewrite to php, then it is only in the evening. - KoVadim
  • I would be extremely grateful !!! - deivan_
  • Excellent! Many thanks !!! Went to cut .. - deivan_