Escaping special character when generating an XML in Java

前端 未结 3 1756
甜味超标
甜味超标 2020-12-24 11:58

I am trying to develop an XML export feature to give my application users to export their data in an XML format. I have got this feature ready and working until it started f

3条回答
  •  半阙折子戏
    2020-12-24 12:18

    You can use apache common lang library to escape a string.

    org.apache.commons.lang.StringEscapeUtils
    
    String escapedXml = StringEscapeUtils.escapeXml("the data might contain & or ! or % or ' or # etc");
    

    But what you are looking for is a way to convert any string into a valid XML tag name. For ASCII characters, XML tag name must begin with one of _:a-zA-Z and followed by any number of character in _:a-zA-Z0-9.-

    I surely believe there is no library to do this for you so you have to implement your own function to convert from any string to match this pattern or alternatively make it into a value of attritbue.

    0.0
    

提交回复
热议问题