0

Say I have the below xml string :

<root>  
 <a>  
     <c>  </c>
 </a>

 <b>  </b>

</root>

Say i want immediate children of 'root' my approach :

  1. Extract tags inside root by using regex:

    extractedString = myXmlString.match(/<(\w+)([^>])>([\s\S])</\1>/g)[3]

  2. use a regex to get the child tags , but it gives me all the children i use this regex on the extracted string (i.e. the string between the root tags)

    childArray = extractedString.match(/<\w+/g)

but the above one also returns me c which is obvious , is there any way of extracting only a and b . or is this approach wrong? I am not looking forward to use any library please do correct me if my approach is wrong towards this (i.e. using regex) and suggest the correct way of doing this.

Rishul Matta
  • 3,383
  • 5
  • 23
  • 29
  • What is the language you are using? If it is JS, this post might be helpful: http://stackoverflow.com/questions/17604071/parse-xml-using-javascript – nhahtdh Nov 03 '14 at 10:14
  • It might be possible to do it by checking for a single TAB at the beginning of the line. – Bifrost Nov 03 '14 at 10:15
  • I am using javascript true but I do not want to use DOM parser am just trying this out as an exercise. So I want to know about the approach and code behind it. – Rishul Matta Nov 03 '14 at 10:19
  • @RishulMatta: If you are not using the DOM parser, then you are left with the option of reinventing the wheel or write a brittle regex that can break at the slightest change. – nhahtdh Nov 03 '14 at 10:20
  • any pointer will be helpful so please suggest the correct way of dealing with it , should i write my own parser? what sources can i refer ? etc etc pls help – Rishul Matta Nov 03 '14 at 10:31

0 Answers0