2011-03-24 51 views
5

这里是我的代码:如果在SAX中设置setNamespaceAware(true),如何获得“xmlns:XXX”属性?

path = wsdlPath; 
SAXParserFactory saxfac = SAXParserFactory.newInstance(); 
saxfac.setNamespaceAware(true); 
saxfac.setXIncludeAware(true); 
saxfac.setValidating(false); 
SAXParser saxParser = saxfac.newSAXParser(); 
saxParser.parse(wsdlPath, this); 

设置setNamespaceAware=true后,我无法在方法public void startElement(String uri, String localName, String qName, Attributes attributes)的参数attributesxmlns:XXX属性。

以下节点:

<definitions name="Service1" 
    targetNamespace="http://www.test.com/service" 
    xmlns="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
    xmlns:tns="http://www.test.com/"> 

我只是得到nametargetNamespace属性。 xmlns,xmlns:wsdl,xmlns:mime,xmlns:httpxmlns:tnsattributes参数中。但他们无法访问。

有什么方法可以使用setNamespaceAware=true并获得节点的所有属性?

回答

7

如果您的XML解析器是XML名称空间感知的,那么您不应该需要访问这些属性,因为它们只定义XML中使用的名称空间的短名称。

在这种情况下,您总是使用全名引用名称空间(例如http://schemas.xmlsoap.org/wsdl/),并且可以忽略在XML中别名的短名称(例如wsdl)。

该SAX不提供这些值的事实被记录在Attributes class

它将[...]不含有用作命名空间声明(xmlns*)属性,除非http://xml.org/sax/features/namespace-prefixes功能设置为true(默认为false)。

因此使用saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true)应该可以帮助您获得这些值。

+0

它的作品!许多许多感谢你! – DeepNightTwo 2011-03-24 09:24:23

+0

我尝试使用这个建议,发现我也必须做以下操作:'saxfac.setFeature(“http://xml.org/sax/features/namespaces”,false);' – 2011-04-06 17:05:57

相关问题