XML data

The following code is an example of XML data that you might need to store using a Java string:

<plastic id="98751"> 
  <singleuse> 
    <item value="Water Bottle" replaceWith="Steel bottle" /> 
    <item value="Straw" replaceWith="Ban Straws" /> 
    <item value="spoon" replaceWith="Steel Spoon" /> 
  </singleuse> 
</plastic> 

The following code shows how you can define the preceding data as a String literal by using the appropriate escape sequences:

String data = 
"<plastic id="98751">
" + 
  "<singleuse>
" + 
    "<item value="Water Bottle" replaceWith="Steel bottle" />
" + 
    "<item value="Straw" replaceWith="Ban Straws" />
" + 
    "<item value="spoon" replaceWith="Steel Spoon" />
" + 
  "</singleuse>
" + 
"</plastic>"; 

Again, the escape sequences added to the preceding code ( to escape " and to add newline) make it very difficult to read and understand the code. The following example shows how you can drop the programming-specific details from the data by using raw string literals:

String dataUsingRawStrings =  
``` 
<plastic id="98751"> 
  <singleuse> 
    <item value="Water Bottle" replaceWith="Steel bottle" /> 
    <item value="Straw" replaceWith="Ban Straws" /> 
    <item value="spoon" replaceWith="Steel Spoon" /> 
  </singleuse> 
</plastic> 
```;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.50