1.下载原版AXMLPrinter2.jar
提取码:74zb
或者下载加强版AXMLPrinter2.jar
提取版:gacz
2.将jar包引入项目。
3.直接在控制台打印
public static void main(String[] arguments) {
String path = "D:/DATA/AndroidManifest.xml";
AXMLPrinter.main(new String[]{path});
}
4.返回字符串,加强版有
public static void main(String[] args) {
String path = "D:/DATA/AndroidManifest.xml";
String xml = ParseXml.parse(path);
System.out.println(xml);
}
5.输出到指定文件,加强版有
public static void main(String[] args) {
String inPath = "D:/DATA/AndroidManifest.xml";
String outPath = "D:/DATA/parse.xml";
try {
ParseXml.export(inPath,outPath);
} catch (IOException e) {
e.printStackTrace();
}
}
第4,第5步是自己写的,下载加强版,或者复制下面的源码配合原版jar包使用
import android.content.res.AXmlResourceParser;
import java.io.*;
public class ParseXml {
private static final float[] RADIX_MULTS = new float[]{0.00390625F, 3.051758E-5F, 1.192093E-7F, 4.656613E-10F};
private static final String[] DIMENSION_UNITS = new String[]{"px", "dip", "sp", "pt", "in", "mm", "", ""};
private static final String[] FRACTION_UNITS = new String[]{"%", "%p", "", "", "", "", "", ""};
/**
* 导出
* @param inPath 来源路径
* @param outPath 输出路径
* @throws IOException
*/
public static void export(String inPath,String outPath) throws IOException {
String xml = parse(inPath);
FileOutputStream out = new FileOutputStream(outPath);
out.write(xml.getBytes());
out.close();
}
/**
* 解析为字符串
* @param path 来源路径
* @return
*/
public static String parse(String path) {
StringBuilder builder = new StringBuilder();
if (path ==null) {
builder.append(log("Usage: AXMLPrinter <binary xml file>"));
} else {
try {
AXmlResourceParser parser = new AXmlResourceParser();
parser.open(new FileInputStream(path));
StringBuilder indent = new StringBuilder(10);
String var3 = "\t";
while(true) {
while(true) {
int type = parser.next();
if (type == 1) {
return builder.toString();
}
switch(type) {
case 0:
builder.append(log("<?xml version=\"1.0\" encoding=\"utf-8\"?>"));
builder.append("\n");
case 1:
default:
break;
case 2:
builder.append(log("%s<%s%s", indent, getNamespacePrefix(parser.getPrefix()), parser.getName()));
builder.append("\n");
indent.append("\t");
int namespaceCountBefore = parser.getNamespaceCount(parser.getDepth() - 1);
int namespaceCount = parser.getNamespaceCount(parser.getDepth());
int i;
for(i = namespaceCountBefore; i != namespaceCount; ++i) {
builder.append(log("%sxmlns:%s=\"%s\"", indent, parser.getNamespacePrefix(i), parser.getNamespaceUri(i)));
builder.append("\n");
}
for(i = 0; i != parser.getAttributeCount(); ++i) {
builder.append(log("%s%s%s=\"%s\"", indent, getNamespacePrefix(parser.getAttributePrefix(i)), parser.getAttributeName(i), getAttributeValue(parser, i)));
builder.append("\n");
}
builder.append(log("%s>", indent));
builder.append("\n");
break;
case 3:
indent.setLength(indent.length() - "\t".length());
builder.append(log("%s</%s%s>", indent, getNamespacePrefix(parser.getPrefix()), parser.getName()));
builder.append("\n");
break;
case 4:
builder.append(log("%s%s", indent, parser.getText()));
builder.append("\n");
}
}
}
} catch (Exception var8) {
var8.printStackTrace();
}
}
return builder.toString();
}
private static String getNamespacePrefix(String prefix) {
return prefix != null && prefix.length() != 0 ? prefix + ":" : "";
}
private static String getAttributeValue(AXmlResourceParser parser, int index) {
int type = parser.getAttributeValueType(index);
int data = parser.getAttributeValueData(index);
if (type == 3) {
return parser.getAttributeValue(index);
} else if (type == 2) {
return String.format("?%s%08X", getPackage(data), data);
} else if (type == 1) {
return String.format("@%s%08X", getPackage(data), data);
} else if (type == 4) {
return String.valueOf(Float.intBitsToFloat(data));
} else if (type == 17) {
return String.format("0x%08X", data);
} else if (type == 18) {
return data != 0 ? "true" : "false";
} else if (type == 5) {
return Float.toString(complexToFloat(data)) + DIMENSION_UNITS[data & 15];
} else if (type == 6) {
return Float.toString(complexToFloat(data)) + FRACTION_UNITS[data & 15];
} else if (type >= 28 && type <= 31) {
return String.format("#%08X", data);
} else {
return type >= 16 && type <= 31 ? String.valueOf(data) : String.format("<0x%X, type 0x%02X>", data, type);
}
}
private static String getPackage(int id) {
return id >>> 24 == 1 ? "android:" : "";
}
private static String log(String format, Object... arguments) {
return String.format(format,arguments);
}
public static float complexToFloat(int complex) {
return (float)(complex & -256) * RADIX_MULTS[complex >> 4 & 3];
}
}