How to tell if a string is xml?

后端 未结 7 1744
北荒
北荒 2021-01-11 10:44

We have a string field which can contain XML or plain text. The XML contains no header, and no root element, i.e. is not well formed.

We need t

7条回答
  •  我在风中等你
    2021-01-11 11:16

    If the XML contains no root element (i.e. it's an XML fragment, not a full document), then the following would be perfectly valid sample, as well - but wouldn't match your detector:

    foobaz
    

    In fact, any text string would be valid XML fragment (consider if the original XML document was just the root element wrapping some text, and you take the root element tags away)!

提交回复
热议问题