2

Consider the following XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView2" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2" />
</LinearLayout>

I would like to read this file line by line similar to reading a text file. I am not looking for an XML parser. How can I do this?

I managed to use XmlResourceParser, but there is a problem. I don't want to parse field by field, I just want to pull all data out.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
letz
  • 1,762
  • 1
  • 20
  • 40
  • may i kindly ask to specify your question more please ? This whole xml is a string, and you would like to read/parse ? – narancs Jun 22 '15 at 13:53
  • What *exactly* are you asking? What do you want to do and why a String? – codeMagic Jun 22 '15 at 13:53
  • @codeMagic imagine that I want to send the xml layout to another service – letz Jun 22 '15 at 13:56
  • You still aren't explaining enough. What would you do with it in that service and why would you need to? From what I'm reading in your question, you are likely trying to do something in a way that you shouldn't be. – codeMagic Jun 22 '15 at 13:57
  • @codeMagic in the same way you can read/open a raw resource, how can I do it with a layout resource? – letz Jun 22 '15 at 14:02
  • Do you want to read the XML layout using a webservice which is sent from the server? – AL̲̳I Jun 22 '15 at 14:10
  • @Ali, no, i just want to read the resource. – letz Jun 22 '15 at 14:12
  • if you want to extract the xml-resource from the android-apk file this is a duplicate of [How to read compiled xml files](http://stackoverflow.com/questions/9480704/how-to-read-compiled-xml-files/11386195) – k3b Jun 22 '15 at 14:52
  • @k3b I want to read it in app run time, I'm not trying to reverse engineer an apk. – letz Jun 22 '15 at 14:57
  • do you want to write an android app that can read other apk-s-layout-xml-s? can you tell us what you already have and what stopped you? by add the missing problem details from the comments to the question will improve the likelyhood that other readers can help you finding a solution. – k3b Jun 22 '15 at 15:08
  • Are you trying to read the text from a TextView? What do you need to read from the XML file? – AdamMc331 Jun 22 '15 at 16:52
  • @McAdam331 I want to read all the content from the layout – letz Jun 22 '15 at 16:53
  • So you want to read from the XML file, in the same way you might read from a text file or something? – AdamMc331 Jun 22 '15 at 16:55
  • @McAdam331 line by line for example, I manage to use XmlResourceParser, but there is a problem, i don't want to parse field by field, I just want to pull all data out – letz Jun 22 '15 at 16:57
  • @letz I believe I understand now. I think your question was very unclear. I have tried to edit it. If it is not what you intended, please edit with your exact intentions and try to give an example of how/why you would use it so it is easier for others to understand. – AdamMc331 Jun 22 '15 at 17:01
  • @McAdam331 thanks, that is exactly what I as trying to ask – letz Jun 22 '15 at 17:03
  • you can't, the xml files are compiled in a binary-like format during compilation of the app – njzk2 Jun 22 '15 at 18:35
  • @njzk2 so only with the parser? – letz Jun 22 '15 at 20:07

2 Answers2

1

It isn't possible when the xml is located in res/layout folder, the xml files are compiled in a binary-like format during compilation of the app.

letz
  • 1,762
  • 1
  • 20
  • 40
0

You can store xml files as assets without them being compiled. You need to store them in the res/raw directory. For example, take a look at the following tutorial

http://thedevelopersinfo.com/2009/11/27/using-files-as-raw-resources-in-android/

Brian Attwell
  • 9,239
  • 2
  • 31
  • 26