0

I have an XML which is a response from a third party API and can have multiple root elements.

Is there a way to check that XML response contains multiple root elements, so that I can wrap it in a parent element else parse it normally.

I know how to handle if there are multiple root elements but how to know if they are in multiple as currently when load the xml document in doc.load it gives me error.

The API response can be with multiple root elements or sometimes it can be a single root element.

Possible response can be :

<myroot>
   <foo>
   </foo>
</myroot>
<myotherroot>
   <bar>
   </bar>
</myotherroot>

or

<parent>
<myroot>
   <foo>
   </foo>
</myroot>
<myotherroot>
   <bar>
   </bar>
</myotherroot>
</parent>
  • 2
    there is always one root and only one. There can't be many roots. So you have to grab that XML and add the root yourself and the internal elements will be children of the root – Dalorzo Mar 18 '16 at 16:52
  • This answer should help you: http://stackoverflow.com/a/6115583/2290059 – Yacoub Massad Mar 18 '16 at 16:55
  • @YacoubMassad yes I know that there can't be many root elements but what if the XML has no root element, in this case i get error saying "multiple root elements" so how to check if XML is wrapped in a Root or not. – user3611646 Mar 19 '16 at 04:21

0 Answers0