First, I have an xml file in res/xml
<?xml version="1.0" encoding="utf-8"?>
<rootelement1>
<subelement>
Hello XML Sub-Element 1
</subelement>
<subelement>
Hello XML Sub-Element 2
<subsubelement>Sub Sub Element</subsubelement>
</subelement>
</rootelement1>
I need to read all content of this file into string value (like read text file to the end and put to string)
I use XmlResourceParser but it does not help.
StringBuffer stringBuffer = new StringBuffer();
Resources res = context.getResources();
XmlResourceParser xpp = res.getXml(R.xml.myxmlfile);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
//add all text line by line, especially "<,>", attribute...
}
How can I solve that? Thanks in advance!
Why are you using XMLResourceParser when you have to read it as a text file? Why not simply use a BufferedReader with StringBuilder? However, to read a file from xml resources folder, try moving it to the raw, or (if by any means possible) to the assets folder first.
Edit:
To copy the respective file, you may use this link: Copying XML from XML Resource to Device Storage