Hen*_*ine 0 java xml xmlpullparser
在我的 XML 中,有一个带有属性的标签,该属性的名称中带有冒号:
<GGS:bericht StUF:bestandsnaam="bestand.txt" >
Run Code Online (Sandbox Code Playgroud)
我已经尝试了所有这些组合来尝试返回此属性的值:
parser.getAttributeValue(null, "StUF:bestandsnaam");
parser.getAttributeValue("StUF", "bestandsnaam");
parser.getAttributeValue(null, "bestandsnaam");
parser.getAttributeValue("bestandsnaam", "StUF");
Run Code Online (Sandbox Code Playgroud)
...但它们都返回 null。
如果我手动删除属性名称的“StUF:”部分,它可以通过调用以下命令来工作:
parser.getAttributeValue(null, "bestandsnaam");
Run Code Online (Sandbox Code Playgroud)
那么如何获取这样一个属性的值呢?也就是说,不使用 getAttributeValue() 的 int 参数版本。
“名称中带有冒号”表示该属性位于命名空间中。在 XML 文档的更深处,您应该在该元素的祖先之一上找到一个命名空间声明,如下所示
xmlns:StUF="{something}"
Run Code Online (Sandbox Code Playgroud)
您需要将其作为“命名空间”参数传递{something}(可能看起来像 HTTP URL 或 a )。urn:...例如,如果您有:
<root xmlns:GSS="urn:example:GSS" xmlns:StUF="http://stuff.com/namespace">
<GGS:bericht StUF:bestandsnaam="bestand.txt" >
Run Code Online (Sandbox Code Playgroud)
那么代码需要是
parser.getAttributeValue("http://stuff.com/namespace", "bestandsnaam");
Run Code Online (Sandbox Code Playgroud)