Thursday 16 June 2016

Writing a parser for XML 5

Previously I discussed how I needed to and did in fact manipulate the input string from the XML document to make it acceptable to my program. So after doing that there comes the question of whether I can display that parsed and stored XML document in the same way in which I am using it? The answer is NO. A strict no.

Whenever one opens up an XML file in the browser, they see a nicely formatted hierarchy of the elements. And then there is always the option of using CSS with the XML file to make it even more appealing to the eye. I did not do any of this. All I wanted was to be able to present that nice hierarchy. That is it.

Printing the stored content was not difficult at all. All it took was a simple recursive function that indented a step further every time it is called with the next generation of elements. Of course this would require the function to know what the level of indentation was in the previous step or hierarchy. And hence to my function I had to pass a string as well that only contained the number of spaces/ tabs there needs to be for that particular call to the function. This way I am able to set the next generation by one extra level of indentation then the present generation by simply calling the function with some added spaces. 

No comments:

Post a Comment