2

我正在使用 SAX 来解析一些 XML。在我的处理程序的startElement()方法中,我试图读取一个名为的属性的值,xsi:type例如:

String type = attributes.getValue("xsi:type");

但是,它总是返回null。这适用于其他一切,所以我假设这是由于命名空间前缀。我怎样才能得到这个值?

4

2 回答 2

3

可能这会有所帮助,试着玩一点。这将返回找到的属性的名称和值,这对于查找用于查询的名称很有用。

if (attributes.getLength() > 0) {
  for (int i = 0; i < attributes.getLength(); i++) {
    System.out.print  ("name: " + attributes.getQName(i)));
    System.out.println(" value: " + attributes.getValue(i)));  
  }
}

也看看这里这里检查功能:getURI

于 2010-01-26T00:04:37.077 回答
1

尝试询问 SAX 它认为属性的 qName 是什么:

for (int i=0; i < attributes.getLength(); i++) {
    String qName = attributes.getQName(i);
    System.out.println("qName for position " + i + ":  " + qName);
}
于 2010-01-26T00:07:24.993 回答