Java parse xml with undeclared namespace

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.


Your problem is not with namespacing. It's with prefixing. Try changing:

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

to this:

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

Not having seen the associated schema, I can't tell if the namespaced schema contains a matching attribute descriptor on the manifest element. I don't know if some of the classes do transparent schema validation. You don't seem to be doing it explicitly in your code so this may be moot.

Somewhere in the data that you receive from these sources, they should tell you what the prefix is that they're using (e.g., "data," "manifest," "http," etc.) in their definition. That'll save you having to hard-code in the, "data," prefix.