XML Error: There are multiple root elements

前端 未结 4 1085
悲哀的现实
悲哀的现实 2020-11-27 19:26

I am getting XML from a web service. Here is what the XML looks like:


    
        Text
    

4条回答
  •  独厮守ぢ
    2020-11-27 19:55

    You need to enclose your elements in a surrounding element as XML Documents can have only one root node:

     
        
            
                Text
            
        
        
            
                
                    Text
                
                
                    Text
                
            
            
                Text
            
        
     
    

    As you're receiving this markup from somewhere else, rather than generating it yourself, you may have to do this yourself by treating the response as a string and wrapping it with appropriate tags, prior to attempting to parse it as XML.

    So, you've a couple of choices:

    1. Get the provider of the web service to return you actual XML that has one root node
    2. Pre-process the XML, as I've suggested above, to add a root node
    3. Pre-process the XML to split it into multiple chunks (i.e. one for each node) and process each as a distinct XML Document

提交回复
热议问题