Load xml external files in runtime

13 views
Skip to first unread message

Matias Furszyfer

unread,
Sep 20, 2015, 3:27:44 AM9/20/15
to Android Australia
Hi everybody, i m building an app and i need to load my xml files from extenal files and not using R class. 

i am thinking to use the method below, but i need to know which encoding of the layout i have to pass as a parameter.

if someone could help i would be very gratefull. thanks

public static XmlPullParser getParser(String xml) {
try {
byte[] data = Base64.decode(xml, Base64.DEFAULT);
// XmlBlock block = new XmlBlock(LAYOUT.getBytes("UTF-8"));
Class<?> clazz = Class.forName("android.content.res.XmlBlock");
Constructor<?> constructor = clazz.getDeclaredConstructor(byte[].class);
constructor.setAccessible(true);
Object block = constructor.newInstance(data);
// XmlPullParser parser = block.newParser();
Method method = clazz.getDeclaredMethod("newParser");
method.setAccessible(true);
return (XmlPullParser) method.invoke(block);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (java.lang.InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
Reply all
Reply to author
Forward
0 new messages