質問

I am writing some code to send an XML document to a Servlet. For one of the XML tag fields, I need to fill it with a string that is retrieved from an external file.

I have found a couple of external files that contain some < and > characters. The servlet will not accept this XML document in this case.

If I remove the < and > characters from the XML tag field, the XML document is sent correctly.

As I am going to be using 1000s of external files, I am sure there will be other occurances of "illegal" characters. Is there an XML encode or similar function that can be used to format a string such that it can be stored in an XML tag with no errors?

I have tried HTML encode, but this does not work. Is there an equivilent action for XML?

役に立ちましたか?

解決

If you really want to build your own XML strings, put your external character in a CDATA tag. You just need to make sure that the end sequence (which is ]]>) is not in the external file. If you find this, you have to encore or replace that with some other string before. So:

<![CDATA[*your external stuff containing < and > here*]]>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top