user2504767
user2504767

Reputation:

How can I save this token as a string value in Java?

I have the following code:

String xmlHeader = "<?xml version="1.0" encoding="UTF-8"?>";

I can't save this token as a string value, how can I solve this problem?

Upvotes: 1

Views: 201

Answers (6)

user2573107
user2573107

Reputation:

You need to escape double quotes by backslashes:

String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

Upvotes: 2

kun tang
kun tang

Reputation: 532

also you can write like this:

String xmlHeader = "<?xml version='1.0' encoding='UTF-8'?>";

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35577

String str1="<?xml version='1.0"encoding='UTF-8"?>"

use

String str1 = "<?xml version=\'1.0\"encoding=\'UTF-8\"?>";

For your case use

String str2 = "<?xml version=\"1.0\"encoding=\"UTF-8\"?>";

Upvotes: 0

M Abbas
M Abbas

Reputation: 6479

If you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. In your example, to save this Token as a String value

String xmlHeader = "<?xml version="1.0" encoding="UTF-8"?>";

you would write

String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234795

For this particular case the quotation characters need to be escaped by prefixing with \:

String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

But note that in XML, you need to use &quot; to escape the quotation character in an attribute value:

When do I need to use the &quot; in xml?

Upvotes: 3

Jean Logeart
Jean Logeart

Reputation: 53839

You need to escape the " with \:

String xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

Upvotes: 7

Related Questions