0

I am trying to read a layout XML file.

String file="res/layout/activity_main.xml";
InputStream in=mCurrentActivity.getClass().getClassLoader().getResourceAsStream(file);
                InputStreamReader isr= new InputStreamReader(in);
                BufferedReader br = new BufferedReader(isr);
                while((line=br.readLine()) != null )
                {
                    XposedBridge.log(line);
                 }

This is my code and this one is my corresponding XML file the output that I get: the output that I get:

Since its my term project I need to figure it out fast(have 2 days left) so any help would be appreciated..

SOLUTION

I didn't have the id so first I get the id with :

int layoutId =mCurrentActivity.getResources().getIdentifier("activity_main", "layout",mCurrentActivity.getPackageName());

and then :

XmlResourceParser s = mCurrentActivity.getResources().getLayout(layoutId);

Thanks to Mike M.

  • That's not unexpected. Layouts are compiled into a special binary format when they're packed into your apk. You could use `Context#getResources.getLayout(R.layout.activity_main)` to get an `XmlResourceParser`, and then loop over that to get all the tags and attributes and values. It's not terribly complicated, but it does take a little work. There's probably even an existing solution out there somewhere. – Mike M. Jan 12 '19 at 21:15
  • I should mention, though, that that's only give you the layout for the current configuration. If you need to access layouts in other configurations, you'd have to do it a little differently. – Mike M. Jan 12 '19 at 21:24
  • first of all thanks for fast answer the problem is i only have the activityr object so are there anyway to get it with an activity object? – myprogramistoobusywitherrors Jan 12 '19 at 21:37
  • i use xposed and hook application and get the activity wich means that i need to do it without id too(R.layout. ...) – myprogramistoobusywitherrors Jan 12 '19 at 21:40
  • so even if i get the context, i need to get layout without id – myprogramistoobusywitherrors Jan 12 '19 at 22:05
  • @MikeM. maybe the other way that you mentioned is the key? – myprogramistoobusywitherrors Jan 12 '19 at 22:38
  • An `Activity` is a `Context`, so you can call `getResources()` on that; e.g., `mCurrentActivity.getResources()...`. And, if you know the name of the layout in that app, you can use the `Resources#getIdentifier()` method to get the numerical ID for it; e.g., `int layoutId = mCurrentActivity.getResources().getIdentifier("activity_main", "layout", mCurrentActivity.getPackageName())`;. – Mike M. Jan 12 '19 at 23:33
  • 1
    Thanks A lot MIKE !!!!!! – myprogramistoobusywitherrors Jan 13 '19 at 16:46

2 Answers2

0

Search gets me quite similar problem here. The main idea for solving your problem, I guess, is that you should specify encoding.

Accordingly to your case try to change the code in that way:

String file="res/layout/activity_main.xml";
InputStream in=mCurrentActivity.getClass().getClassLoader().getResourceAsStream(file);
InputStreamReader isr= new InputStreamReader(in, "UTF-8");
BufferedReader br = new BufferedReader(isr);
while((line=br.readLine()) != null)
{
    XposedBridge.log(line);
}

or

String file="res/layout/activity_main.xml";
InputStream in=mCurrentActivity.getClass().getClassLoader().getResourceAsStream(file);
InputStreamReader isr= new InputStreamReader(in, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
while((line=br.readLine()) != null)
{
    XposedBridge.log(line);
}

or try to use another encoding variant despite UTF-8.

I suppose it depends on what type of encoding did you set in your.xml file

Example:

<?xml version="1.0" encoding="utf-8"?>
aLT
  • 326
  • 3
  • 9
  • Encoding is not the problem. The "XML" file is not plain XML but binary XML. Therefore simply changing the encoding does not change anything! – Robert Jan 13 '19 at 11:44
0

The XML file you are trying to read is not plain XML bin binary XML format. Therefore trying to read it via common Reader will not work.

You can use e.g. the library AXML to load such binary xml files:

String file="res/layout/activity_main.xml";
InputStream in = mCurrentActivity.getClass().getClassLoader().getResourceAsStream(file));
Document doc = new CompressedXmlParser().parseDOM(in);
Robert
  • 39,162
  • 17
  • 99
  • 152