How to parse a structured (XML Like) texts in JavaScript?

I have a text data with nested format, like an XML format but is using brackets {} instead of <>. What would be the best way to parse this, maybe convert it to XML format? I will then manipulate it like display it on a page or save the info on data base.

This is how it looks like:

{myItem num="345"}
{subItem num="1"}
My Sub item Texts
{subItem num="2"}
My Sub item Texts

As you can see, it looks like an XML format but it uses brackets.

17 thoughts on “How to parse a structured (XML Like) texts in JavaScript?”

  1. read all the data into a string and use the replace function to replace all the ‘{‘ with a ‘<‘ and the same for ‘}’ with a ‘>’

    var str = "{myItem num="345"}{subItem num="1"}My Sub item Texts {/subItem}{subItem 
               num="2"}My Sub item Texts{/subItem}{/myItem}";
    var res = str.replace("{", "<");
    var res2use = res.replace("}", ">");
  2. Unless this "structured" language has a specification, it is not a formal language, and the parsing of it may tend more toward Natural Language Processing (NLP) than markup or programming language parsing due to the lack of a fixed grammar to follow.

    Lexically replacing { and } with < and > and calling it XML presumes much, including:

    • Do { and } only appear as markup?
    • Are all other XML well-formedness rules followed?

    As a very rough first cut, you might apply the above replacements and then follow the guidance provided by How to parse invalid (bad / not well-formed) XML?


Leave a Comment