【问题标题】:Getting values from SAX attributes when namespaces are involved涉及命名空间时从 SAX 属性中获取值
【发布时间】:2010-01-25 23:47:35
【问题描述】:

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

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

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

【问题讨论】:

    标签: java xml sax


    【解决方案1】:

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

    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)));  
      }
    }
    

    还可以看看herehere查看函数:getURI

    【讨论】:

      【解决方案2】:

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

      for (int i=0; i < attributes.getLength(); i++) {
          String qName = attributes.getQName(i);
          System.out.println("qName for position " + i + ":  " + qName);
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-28
        • 1970-01-01
        • 2021-09-30
        • 1970-01-01
        • 2019-07-03
        • 2012-11-10
        • 2012-01-22
        • 1970-01-01
        • 2021-12-14
        相关资源
        最近更新 更多