Java Code Examples for android.content.res.AXmlResourceParser#next()

The following examples show how to use android.content.res.AXmlResourceParser#next() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AndroidXmlConvertor.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static void convert(InputStream is, PrintWriter out, TaskMonitor monitor)
		throws IOException, CancelledException {

	monitor.setMessage("Converting Android Binary XML to Text...");

	AXmlResourceParser parser = new AXmlResourceParser();
	parser.open(is);

	try {
		int indent = -1;
		int type;
		while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
			monitor.checkCanceled();

			StringBuffer buffer = new StringBuffer();
			switch (type) {
				case XmlPullParser.START_DOCUMENT:
					buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
					buffer.append("\n");
					break;
				case XmlPullParser.START_TAG: {
					++indent;

					buffer.append(StringUtilities.pad("", '\t', indent));
					buffer.append("<");
					buffer.append(getNamespacePrefix(parser.getPrefix()));
					buffer.append(parser.getName());
					buffer.append("\n");

					int namespaceCountBefore = parser.getNamespaceCount(parser.getDepth() - 1);
					int namespaceCount = parser.getNamespaceCount(parser.getDepth());

					++indent;

					for (int i = namespaceCountBefore; i != namespaceCount; ++i) {
						buffer.append(StringUtilities.pad("", '\t', indent));
						buffer.append("xmlns:");
						buffer.append(parser.getNamespacePrefix(i));
						buffer.append("=");
						buffer.append("\"");
						buffer.append(parser.getNamespaceUri(i));
						buffer.append("\"");
						buffer.append("\n");
					}

					for (int i = 0; i < parser.getAttributeCount(); ++i) {
						buffer.append(StringUtilities.pad("", '\t', indent));
						buffer.append(getNamespacePrefix(parser.getAttributePrefix(i)));
						buffer.append(parser.getAttributeName(i));
						buffer.append("=");
						buffer.append("\"");
						buffer.append(getAttributeValue(parser, i));
						buffer.append("\"");
						buffer.append("\n");
					}

					buffer.append(StringUtilities.pad("", '\t', indent));
					buffer.append(">");
					buffer.append("\n");

					--indent;
				}
					break;
				case XmlPullParser.END_TAG: {
					buffer.append(StringUtilities.pad("", '\t', indent));
					buffer.append("<");
					buffer.append("/");
					buffer.append(getNamespacePrefix(parser.getPrefix()));
					buffer.append(parser.getName());
					buffer.append(">");
					buffer.append("\n");
					--indent;
				}
					break;
				case XmlPullParser.TEXT: {
					buffer.append(StringUtilities.pad("", '\t', indent));
					buffer.append(parser.getText());
					buffer.append("\n");
				}
			}

			out.print(buffer.toString());
		}
		out.println();
	}
	catch (XmlPullParserException | ArrayIndexOutOfBoundsException e) {
		throw new IOException("Failed to read AXML file", e);
	}
	finally {
		parser.close();
	}
}
 
Example 2
Source File: ResignerLogic.java    From apkReSign with Apache License 2.0 4 votes vote down vote up
public static String AXMLToXML(byte[] axml) throws Exception {
    AXmlResourceParser parser = new AXmlResourceParser();
    ByteArrayInputStream bais = new ByteArrayInputStream(axml);
    parser.open(bais);
    StringBuilder indent = new StringBuilder(10);
    StringBuilder output = new StringBuilder(axml.length * 2);
    final String indentStep = "   ";

    while (true) {
        int type = parser.next();
        if (type == XmlPullParser.END_DOCUMENT) {
            break;
        }
        switch (type) {
            case XmlPullParser.START_DOCUMENT: {
                output.append(String
                        .format("<?xml version=\"1.0\" encoding=\"utf-8\"?>"));
                output.append("\n");
                break;
            }
            case XmlPullParser.START_TAG: {
                output.append(String.format("%s<%s%s", indent,
                        getNamespacePrefix(parser.getPrefix()),
                        parser.getName()));
                output.append("\n");
                indent.append(indentStep);

                int namespaceCountBefore = parser.getNamespaceCount(parser
                        .getDepth() - 1);
                int namespaceCount = parser
                        .getNamespaceCount(parser.getDepth());
                for (int i = namespaceCountBefore; i != namespaceCount; ++i) {
                    output.append(String.format("%sxmlns:%s=\"%s\"", indent,
                            parser.getNamespacePrefix(i),
                            parser.getNamespaceUri(i)));
                    output.append("\n");
                }

                for (int i = 0; i != parser.getAttributeCount(); ++i) {
                    output.append(String.format("%s%s%s=\"%s\"", indent,
                            getNamespacePrefix(parser.getAttributePrefix(i)),
                            parser.getAttributeName(i),
                            getAttributeValue(parser, i)));
                    output.append("\n");
                }
                output.append(String.format("%s>", indent));
                output.append("\n");
                break;
            }
            case XmlPullParser.END_TAG: {
                indent.setLength(indent.length() - indentStep.length());
                output.append(String.format("%s</%s%s>", indent,
                        getNamespacePrefix(parser.getPrefix()),
                        parser.getName()));
                output.append("\n");
                break;
            }
            case XmlPullParser.TEXT: {
                output.append(String.format("%s%s", indent, parser.getText()));
                output.append("\n");
                break;
            }
        }
    }
    return output.toString();
}