You need to enclose your <parent> elements in a surrounding element as XML Documents can have only one root node:<parents> <!-- I've added this tag --> <parent> <child> Text </child> </parent> <parent> <child> <grandchild> Text </grandchild> <grandchild> Text </grandchild> </child> <child> Text </child> </parent></parents> <!-- I've added this tag -->由于您是从其他地方接收此标记,而不是自己生成它,因此在尝试将其解析为 XML 之前,您可能必须通过将响应视为字符串并用适当的标签包装它来自己执行此操作.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.所以,您有几个选择:让网络服务的提供者返回给你实际具有一个根节点的 XML按照我上面的建议对 XML 进行预处理,以添加根节点预处理 XML 以将其拆分为多个块(即每个 节点一个)并将每个块作为一个不同的 XML 文档进行处理Get the provider of the web service to return you actual XML that has one root nodePre-process the XML, as I've suggested above, to add a root nodePre-process the XML to split it into multiple chunks (i.e. one for each <parent> node) and process each as a distinct XML Document 这篇关于XML 错误:有多个根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 20:14
查看更多