XML : Recursively change a tree in string form to XML

I am doing some work on parsing a block of code which is originally in pascal into javascript. As an added extra I am wanting to show the parse tree in an XML format for example:

  <book id="bk101">    <author>Gambardella, Matthew</author>    <title>XML Developer's Guide</title>    <genre>Computer</genre>    <price>44.95</price>    <publish_date>2000-10-01</publish_date>    <description>An in-depth look at creating applications     with XML.</description>  </book>    

The tree that I receive from the parsing is in this form:

  (program (programHeading PROGRAM (identifier HelloWorld) ;) (block (compoundStatement BEGIN (statements (statement (unlabelledStatement (simpleStatement (output writeln ( (string 'Hello World') ))))) ; (statement (unlabelledStatement (simpleStatement emptyStatement)))) END)) .)    

I know that it is entirely possible but I am struggling to get the output I need, what I've written which doesn't currently work is as follows:

  public String recursiveTree(String ruleName, String tree)  {      int point = 0;      if (tree.charAt(point) == ')')          return "</" +ruleName+ ">\n";      else      {          point++;          while ((tree.charAt(point) != '(' && tree.charAt(point) != ')') && point < tree.length())              point++;          return "<" + ruleName + ">\n" + recursiveTree(tree.substring(1, point), tree.substring(point));      }  }    

The language I am coding in is Java.

EDIT: This was the original pascal program:

  PROGRAM HelloWorld;  BEGIN    writeln('Hello World');  END.    

No comments:

Post a Comment