1
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.

0

4 Answers 4

2

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>";
Sign up to request clarification or add additional context in comments.

3 Comments

Nope, it will not work - Java does not support multiple-line string literals.
Fixed it by removing line breaks.
With one ";" at the end it will be fine. ;)
1

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>" 

Comments

1

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.

Comments

0

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 \

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.