0

i have a very huge HTML string in my app.

When I use it in the code, everything is fine but when I try to declare it in strings.xml, I am getting some errors. Is there a way to make a simple copy of the string in strings.xml? Thank you

10
  • 1
    "Some errors." Not terribly helpful. Is it in a CDATA? Commented Apr 13, 2012 at 11:56
  • why don't you put the string in a file in assets folder? Commented Apr 13, 2012 at 11:59
  • @gabi, how can i do this? in text file, how to open it then? Commented Apr 13, 2012 at 12:00
  • AssetFileDescriptor descriptor = getAssets().openFd("myfile.txt"); FileReader reader = new FileReader(descriptor.getFileDescriptor()); Commented Apr 13, 2012 at 12:05
  • Thank you @gabi, but what to do with the file reader? Commented Apr 13, 2012 at 12:20

2 Answers 2

4

HTML and XML are the same basic language, I do not believe that you can store HTML in a string, why not save the html page and package it with the application?


Save the page as a html page in res > raw and then call this method

 String html = Utils.readRawTextFile(ctx, R.raw.rawhtml);

 public static String readRawTextFile(Context ctx, int resId)
     {
          InputStream inputStream = ctx.getResources().openRawResource(resId);

             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

             int i;
              try {
               i = inputStream.read();
               while (i != -1)
                  {
                   byteArrayOutputStream.write(i);
                   i = inputStream.read();
                  }
                  inputStream.close();
              } catch (IOException e) {
                  return null;
              }
                 return byteArrayOutputStream.toString();
     }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @SmartLemon, how can I do this
Sweet as :D, gotta use it myself now :P
1

Error may come at special characters like @ double quote single quote etc. to overcome it prefix \ to it and your error get resolved

if you assign same string programmatically there also you will find the same issue

String mString= "your huge string with @ error";

in this also you have to overcome be prefixing backslash

 String mString= "your huge string with \@ error";

6 Comments

Thank you, but how to explain that there is no errors when using it in a local variable?
there will be no error in local variable, as in JAVA code.. u dont need to escape single quote, but u have to do it in XML.
How is this going to work if he has a huge string? Shouldn't you just save the html page?
If you are going to do this use find and replace find @ and replace with \@ would be waaay fastr
Also HTML contains things like /> which wouldnt work in xml because it will take it as a end.
|

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.