Shumaila
Shumaila

Reputation: 21

Storing XML as String

String xml ="<results count="6">
        <result>
            <id>1</id>
            <name>Mark</name>
            <score>6958</score>
        </result>
   </results>"

I am trying to store XML data in an string. I have followed the above mentioned syntax.But its not working. Please Help me with this.

Upvotes: 1

Views: 4496

Answers (4)

Garrett Vlieger
Garrett Vlieger

Reputation: 9494

You need to escape the quotes in your XML string and remove the line spaces:

String xml ="<results count=\"6\">" +
        "<result>" +
        "  <id>1</id>" +
        "  <name>Mark</name>" +
        "  <score>6958</score>" +
        "</result>" +
   "</results>";

Upvotes: 2

DejanLekic
DejanLekic

Reputation: 19787

You can't do that in Java. String literal can't span multiple lines. Here is how it is done:

String xml = "<results count=\"6\">"
    + " <result>"
    + "   <id>1</id>"
    + "   <name>Mark</name>"
    + "   <score>6958</score>"
    + " </result>"
    + "</results>";

Also note that any double quote must be escaped.

Upvotes: 1

Sid Malani
Sid Malani

Reputation: 2116

compiler does not like new lines and double quotes. It should be

String xml ="<results count=\"6\">"+
        "<result>" +
            "<id>1</id>" +
            "<name>Mark</name>" +
            "<score>6958</score>" +
        "</result>" +
   "</results>";

double quotes need to be escaped with \

Upvotes: 0

Kashif Khan
Kashif Khan

Reputation: 2645

try to escape the special characters inside the string.....

String xml ="<results count=\"6\">         <result>             <id>1</id>             <name>Mark</name>             <score>6958</score>         </result>    </results>" 

or you can use this

    String xml ="<results count='6'>         <result>             <id>1</id>             <name>Mark</name>             <score>6958</score>         </result>    </results>" 

Upvotes: 1

Related Questions