Decode xml file android

How to Extract XML code from Apk file in Android

Updated on Aug 3, 2019 by Guest Authors

When you lost your data due to some reasons but you have a apk file. Then you can get the code from the apk file. To get the xml code from apk file.

There are two tools you have to need for getting xml code from apk file. These tools are listed following:

Apktool can decode apk file to original form and rebuild them. It makes possible to debug small code step by step. You can download these software from the following links.

There are following some steps to extract XML code from apk file:

Step 1:

First open the new folder and put these files step by step in it. Put apk file (which you want to decode). Now download the apktool.jar from the given link. Put the apktool v1.x and apktool install window in the same folder.

Step 2:

After first step now you have to download “framework-res.apk”. Framework-res.apk file helps you to extract the apk file (which you want to decode) and also extract the resource files. From this we can get our resource files in layouts. You can also download from this link. When download is completed then put “framework-res.apk” file in same folder.

Step 3:

After second step you have to open command prompt. Go to left top of the window click file menu in that you have to click open command prompt. Then command prompt is open. Now in command prompt window you are automatically to the root directory of apktool. In command prompt window you have to write a command “apktool if framework-res.apk” and press enter for further execution. When execution is finished then again you have to write a command “filename.apk” (here filename refers to that file which you want to decode) and press enter execution is start. Now it fetches all the resource files. And new folder is generated as same name of apk (which you want to extract).

Open that folder and now you can see res named folder in it. After that open res folder now you can see different files and folder in it. Now you can see layouts folder in it. Open layouts folder then you can see all the xml resource files in it.

Here you get the xml code files from apk. Now you can easily read the xml files.

Hopefully this information is helpful to you.

Disclosure: As an Amazon Associate, I earn from qualifying purchases. The commission help keep the rest of my content free, so thank you!

Reader Interactions

Comments

Thaks for detailed error information I hope solve my problem and i create my first app

Источник

Decode xml file android

  • Update apktool to v2.2.1
  • apktool empty-framework-dir —force

v2.1.1 -> v2.2.0

  • Run the following commands to migrate your framework directory
  • Apktool will work fine without running these commands, this will just cleanup abandoned files
    • unix
    • windows

v2.0.1 -> v2.0.2

  • Update apktool to v2.0.2
  • Remove framework file $HOME/apktool/framework/1.apk due to internal API update (Android Marshmallow)

v1.5.x -> v2.0.0

  • Java 1.7 is required
  • Update apktool to v2.0.0
  • aapt is now included inside the apktool binary. It’s not required to maintain your own aapt install under $PATH. (However, features like -a / —aapt are still used and can override the internal aapt)
  • The addition of aapt replaces the need for separate aapt download packages. Helper Scripts may be found here
  • Remove framework $HOME/apktool/framework/1.apk
  • Eagle eyed users will notice resources are now decoded before sources now. This is because we need to know the API version via the manifest for decoding the sources

Parameter Changes

  • Smali/baksmali 2.0 are included. This is a big change from 1.4.2. Please read the smali updates here for more information
  • -o / —output is now used for the output of apk/directory
  • -t / —tag is required for tagging framework files
  • -advance / —advanced will launch advance parameters and information on the usage output
  • -m / —match-original is a new feature for apk analysis. This retains the apk is nearly original format, but will make rebuild more than likely not work due to ignoring the changes that newer aapt requires
  • After [d]ecode , there will be new folders (original / unknown) in the decoded apk folder
    • original = META-INF folder / AndroidManifest.xml , which are needed to retain the signature of apks to prevent needing to resign. Used with -c / —copy-original on [b]uild
    • unknown = Files / folders that are not part of the standard AOSP build procedure. These files will be injected back into the rebuilt APK.
  • apktool.yml collects more information than last version
    • SdkInfo — Used to repopulate the sdk information in AndroidManifest.xml since newer aapt requires version information to be passed via parameter
    • packageInfo — Used to help support Android 4.2 renamed manifest feature. Automatically detects differences between resource and manifest and performs automatic —rename-manifest-package on [b]uild
    • versionInfo — Used to repopulate the version information in AndroidManifest.xml since newer aapt requires version information to be passed via parameter
    • compressionType — Used to determine the compression that resources.arsc had on the original apk in order to replicate during [b]uild
    • unknownFiles — Used to record name/location of non-standard files in an apk in order to place correctly on rebuilt apk
    • sharedLibrary — Used to help support Android 5 shared library feature by automatically detecting shared libraries and using —shared-lib on [b]uild
Читайте также:  Как печатать файлы с андроида

Examples of new usage in 2.0 vs 1.5.x

Old (Apktool 1.5.x) New (Apktool 2.0.x)
apktool if framework-res.apk tag apktool if framework-res.apk -t tag
apktool d framework-res.apk output apktool d framework.res.apk -o output
apktool b output new.apk apktool b output -o new.apk

v1.4.x -> v1.5.1

  • Update apktool to v1.5.1
  • Update aapt manually or use package r05-ibot via downloading Mac, Windows or Linux
  • Remove framework file $HOME/apktool/framework/1.apk

Источник

Parsing XML data in Android Apps

Jun 28, 2018 · 4 min read

JSON is become a very widespread format for the Web Services but in some cases, you would have to parse data in the good old XML format. In that tutorial, you are going to learn how to parse a XML file on Android.

Note that there are various XML parses available in the Android SDK in standard. Thus, you can use the following solutions :

  • XMLPullParser API
  • DOM Parser API
  • SAX Parser API

As usual, each parser has its advantages and drawbacks.

The recommendation on Andr o id is to use the XMLPullParser API which consumes less memory than the DOM Parser API which loads the complete XML file in memory before parsing it.

So, in this tutorial, we will use the XMLPullParser API. Note that you can also discover this tutorial in video on YouTube :

Creating a simple User Interface

To display the parsed XML data, we need to create a simple User Interface. It will consist in a simple TextView centered on the screen :

Defining XML data to parse

Then, we define some XML data to parse inside a file under the assets directory of our Android project. This file will be named data.xml and will contain some NBA players with data like name, age and position played :

Creating a Player POJO

To map the data for each player, we create a Player POJO (Plain Old Java Object) :

Preparing the XML Parser

The first step is to load the XML file from the assets of our Android Application. For that, we define a parseXML method. We create a new instance of the XMLPullParserFactory class. Then, we create a new XMLPullParser instance by calling the newPullParser method of the XMLPullParserFactory got previously.

We got an InputStream on the XML file by calling the open method of the AssetManager instance. With that instance, we can set the input on the XMLPullParser instance. It gives us the following code :

Parsing the XML data

Now, we need to parse the XML data. The parsing will be processed in the processParsing method. We start by getting the current event type from the parser by calling the getEventType method.

Then, we enter in a loop while the event type is different of the XmlPullParser.END_DOCUMENT constant. In the loop, when we meet a XmlPullParser.START_TAG event type, we get the name of the current tag by calling the getName method.

When we have a player tag, we create a new Player instance and we add it in an ArrayList of Player. This list will be used to store all the players read from the XML file.

If the current player is not null, we are reading data for a player. We have to check if the tag is name, age or position. For each case, we call the nextText method of the parser to get the value associated to the tag. Then, we set the value on the current player.

At the end of the loop, we have stored all the players from the XML file on the ArrayList of Player. Note that just before the end of the loop, you need to call the next method of the parser to pass to the following event.

It gives us the following code :

Displaying the XML data

The last step is also the simplest. We need to display the players in our TextView via the printPlayers called at the end of the processParsing method.

In that method, we iterate on the ArrayList of Player and for each player we add its properties (name, age and position) on a StringBuilder instance. Finally, we display the text on the TextView by calling its setText method with the value of the StringBuilder instance.

It gives us the following complete code for the MainActivity :

Our App in Action

Best part of the tutorial is coming since it’s time to put our App in Action. When, you will launch the Application, you should have the following result :

That’s all for that tutorial.

To discover more tutorials on Android development, don’t hesitate to subscribe to the SSaurel’s Channel on YouTube :

Источник

Processing and Parsing XML in Android

Sharing data over the internet is very popular. We share our information with the other users over the internet. Let’s take a very common example, since we all are developers and to be more precise, we all are Android Developers and we all must have visited the StackOverflow website whenever we are stuck into some problem in our projects and in every 99 out of 100 cases, we find the solution to our problem. The answer that we get from the StackOverflow website consists of various parts that we are less bothered about. In general, the answers or the questions present on the website contains the id of the question, type of the question, author name of the article or question, publishing data, URL and many other things are there on the website. But we want only meaningful data i.e. we want answers and the authors and nothing else.

The problem arises when you want to access the data of a particular website in your Android Application. This is because the data present on the websites are in JSON format or in XML format. So, in order to use that data or to extract the meaningful information from that data, you have to parse that XML file or a JSON file.

So, in this blog, we will learn how to process and parse the XML data in Android. So, let’s get started.

What is XML?

XML stands for Extensible Markup Language. It is a set of rules that are used to encode data or documents in a Machine-readable language. There are many websites that use an XML format to share data on the internet. For example, many blogging websites use an XML format to share data between users. So, XML parsing and processing become an important task to use that data in our Android Application.

XML elements

In general, an XML file consists of a number of elements that together make an XML file. They are the basic building blocks of XML document. These elements are used to store some text elements, attributes, media, etc. Below is the syntax of XML element:

Here, element-name is the name of the element and attributes are used to define the property of XML element. Following is an example of an XML document that is used to describe the data of the student:

We can broadly divide the XML elements in four parts:

  1. Prolog: It is the first line that contains the information about the XML file i.e. it is the first line of the XML file.
  2. Events: An XML file contains a number of events like document start, document end, Tag start, Tag end, etc.
  3. Text: In between the tags(opening and closing), there can be some text. For example, in the above example, “John” is the text.
  4. Attributes: These are the properties of XML elements.

XML Parsing

So, we have seen the introduction of XML and our next task is, How to parse the XML in Android? So, let’s find out.In Android, there are various parsers that can be used to parse the XML file in Android. Some of these are:

  1. XMLPullParser
  2. DOM Parser
  3. SAX Parser

Among these three parsers, XMLPullParser is the most commonly and most widely used parsers in Android. This is because XMLPullParser is very efficient and easy to use. Also, unlike the DOM Parser which loads the whole file in the memory and then parses it, the XMLPullParser consumes less memory.

Steps involved in XML parsing

Following are the steps that are involved in XML parsing:

  • Analyze the feed: There are many cases when you will be provided with a number of data but you need only few of them. SO, before parsing the XML, your first step should be to find the desired element in the XML data. For example, here is example of XML file of StackOverflow:

Here, if you want to extract the data present in the entry tag and the data of its sub tags then you should keep note of these things.

  • Create XMLPullParser object: Our next step is to create the object of XMLPullParser and specify the file name for the XMLPullParser that contains XML. You can use a file or a stream. Following is the code to create an object of the XMLPullParser and then passing the file name for the XMLPullParser:
  • Read the desired data: Our last step is to parse the data of the XML file. The following readFeed() function is used to parse the XML file and extract only the values of entries and store it into a list:

Here, XMLPullParser.END_TAG is used to find if the parser has reached to the last tag of the file or not. If it has reached then return and it has not reached then we will check for the entry tag and if found then will add the value to the entries list. If the values are not found be to of entries type then we can skip the parser. At last, we return entries.

Following are the methods that can be used while parsing an XML file:

  1. getEventType(): This method is used to get the event type. For example, Document start, Document End, Tag start, Tag end, etc.
  2. getName(): This is used to get the tag name in a file. For example, in the above code, the tag name is entry.
  3. getAttributeValue(): This method is used to get the attribute value of a particular tag.
  4. getAttributeCount(): It returns the total number of attributes of the current tag.
  5. getAttributeName(int index): It returns the attribute name at a particular index.
  6. getColumnNumber(): It returns the current column number using a 0 based indexing.
  7. getName(): It returns the name of the tag.
  8. getText(): It returns the text present in a particular element.

XML parsing example

Now, let’s do one example to understand the concept of XML parsing ina very easy and clear manner.

Open Android Studio and create a project with Empty Activity template.

The very first step is to create the XML file. Either you create your own XML file or you can use mine. To create an XML file, go to java folder and then right click on it. Then click on New > Folder > Assets folder and then OK. One assets folder will be created for you. Now right click on that folder and create a file and name it as example.xml. Following is the XML content of example.xml file:

This is a simple example of student data containing the name, surname, mobile and section of the student. In this example, we will extract the name, surname and section of the student. We will ignore the mobile number.

Now, let’s code for the UI part of our application. Here we will display the desired data on a TextView. So, open the activity_main.xml file and add the below code:

So, we are done with the UI part. Now, let’s write the code for the XML parsing. Below is the code for the MainActivity.kt file:

Now, run the application on your mobile phone and try to add more data to your XML file i.e. the example.xml file.

Conclusion

In this blog, we learned about XML parsing in Android. We learned how to parse the data present in the XML format in Android Application. We saw how to use XMLPullParser to do XML parsing. At last, we did one example of XML parsing. You can get more contents about XML parsing at the Android Developer Website.

Источник

Читайте также:  Android v11 не работает obb
Оцените статью