Java使用未声明的名称空间解析xml [英] Java parse xml with undeclared namespace

查看:205
本文介绍了Java使用未声明的名称空间解析xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次发布​​新的android应用程序时,我需要在我们的网站上更新应用程序信息.因此,我尝试通过上传到服务器时apk中的AndroidManifest.xml来自动更新应用信息.

我使用AXMLResourcejdom2来获取AndroidManifest.xml并进行解析.

final Namespace NS = Namespace.getNamespace("http://schemas.android.com/apk/res/android");

zipFile = new ZipFile(apkPath);
ZipEntry zipEntry = new ZipEntry("AndroidManifest.xml");
inputStream = zipFile.getInputStream(zipEntry);
AXMLResource axmlResource = new AXMLResource();
axmlResource.read(inputStream);

SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(new ByteArrayInputStream(axmlResource.toXML().toString().getBytes("UTF-8")));
Element root = document.getRootElement();
System.out.println(root.getAttributeValue("versionCode",NS));
System.out.println(root.getAttributeValue("versionName",NS));

但我总是得到,

与属性"data:versionCode"相关联的前缀"data" 元素类型为清单"的元素没有绑定

然后我打印AndroidManifest.xml,发现data命名空间未声明.

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    data:versionCode="344"
    data:versionName="3.2.8"

当我检查 android studio文档并发现在那里使用时声明的名称空间.

那我认为这可能是由我们的应用程序开发人员的违规行为引起的.

但是,当我进一步思考时,为什么可以发布带有未声明名称空间的AndroidManifest.xml应用程序,然后我下载了一些apk,例如facebook,youtube,centos,百度磁盘和支付宝,却发现只有centos不使用未声明的名称空间. /p>

对于com.google.android.youtube.apk

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    http:versionCode="1425572340"
    http:versionName="14.25.57"

与属性"http:versionCode"相关联的前缀"http" 元素类型为清单"的元素没有绑定

对于com.facebook.katana-225.0.0.47.118.apk

<?xml version="1.0" encoding="utf-8"?>
<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        manifest:versionCode="158425934"
        manifest:versionName="225.0.0.47.118"

与属性"manifest:versionCode"相关联的前缀"manifest" 元素类型为清单"的元素没有绑定

这是在AndroidManifest.xml中使用未定义名称空间的命令,但是我该如何处理呢?

解决方案

从技术上讲,您的文件是格式正确的XML,但不是名称空间格式正确的XML.绝大多数现代XML工具只能处理(或生成)命名空间格式良好的XML.但是,如果找到的XML解析器仍然可以处理名称空间格式不正确的XML,则可以使用它来以某种满足您需求的方式修复"您的XML(例如,您可以将manifest:versionCode替换为manifest-versionCode).

但是,与其以这种方式修复XML,不如首先创建一个名称空间格式良好的XML,这样会更好,因此您不必受工具选择的限制.

Each time our new android app released, I need to update app info on our website. So I try to update the app info automatically, by read the AndroidManifest.xml in the apk when uploaded to server.

I use AXMLResource and jdom2 to get the AndroidManifest.xml and parse it.

final Namespace NS = Namespace.getNamespace("http://schemas.android.com/apk/res/android");

zipFile = new ZipFile(apkPath);
ZipEntry zipEntry = new ZipEntry("AndroidManifest.xml");
inputStream = zipFile.getInputStream(zipEntry);
AXMLResource axmlResource = new AXMLResource();
axmlResource.read(inputStream);

SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(new ByteArrayInputStream(axmlResource.toXML().toString().getBytes("UTF-8")));
Element root = document.getRootElement();
System.out.println(root.getAttributeValue("versionCode",NS));
System.out.println(root.getAttributeValue("versionName",NS));

But I always get,

The prefix "data" for attribute "data:versionCode" associated with an element type "manifest" is not bound

And I print the AndroidManifest.xml,and found the data namespace is unclared.

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    data:versionCode="344"
    data:versionName="3.2.8"

When I check android studio doc and found there use declared namespace.

Then I think this may be caused by our app developers's Irregularities.

But when I think further, why app with AndroidManifest.xml with undeclared namespace can released, then I download some apks like facebook,youtube,centos,baidu disk and alipay, and found only centos doesn't use undeclared namespace.

For com.google.android.youtube.apk

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    http:versionCode="1425572340"
    http:versionName="14.25.57"

The prefix "http" for attribute "http:versionCode" associated with an element type "manifest" is not bound

For com.facebook.katana-225.0.0.47.118.apk

<?xml version="1.0" encoding="utf-8"?>
<manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        manifest:versionCode="158425934"
        manifest:versionName="225.0.0.47.118"

The prefix "manifest" for attribute "manifest:versionCode" associated with an element type "manifest" is not bound

So here is comman to use undeclased namespace in AndroidManifest.xml,but how can I pase it?

解决方案

Technically your file is well-formed XML but not namespace-well-formed XML. The vast majority of modern XML tools will only handle (or produce) XML that is namespace-well-formed. But if you can find an XML parser that still handles XML that is not namespace-well-formed, then you can use this to "repair" your XML in some way that meets your needs (for example, you could replace manifest:versionCode by manifest-versionCode).

However, rather than repairing the XML in this way, it would be much better to create namespace-well-formed XML in the first place, so you're not constrained in your choice of tools.

这篇关于Java使用未声明的名称空间解析xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆