这是我的代码:
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
,我无法得到xmlns:XXX
参数中的属性attributes
方法的public void startElement(String uri, String localName, String qName, Attributes attributes)
.
对于以下节点:
<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/">
我刚刚得到name
and targetNamespace
属性。xmlns
, xmlns:wsdl
, xmlns:mime
, xmlns:http
and xmlns:tns
都在attributes
范围。但它们是不可访问的。
有什么办法可以使用吗setNamespaceAware=true
并获取节点的所有属性?
当您的 XML 解析器支持 XML 命名空间时,您不应该need访问这些属性,因为它们只定义 XML 中使用的命名空间的短名称。
在这种情况下,您始终使用全名来引用名称空间(例如http://schemas.xmlsoap.org/wsdl/
)并且可以忽略它们在 XML 中的别名(例如wsdl
).
SAX 不提供这些值的事实记录在Attributes class http://download.oracle.com/javase/6/docs/api/org/xml/sax/Attributes.html:
它将 [...] 不包含用作命名空间声明的属性(xmlns*
)除非http://xml.org/sax/features/namespace-prefixes
功能设置为true
(it is false
默认情况下)。
所以使用saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true) http://download.oracle.com/javase/6/docs/api/javax/xml/parsers/SAXParserFactory.html#setFeature%28java.lang.String,%20boolean%29应该可以帮助您实现这些价值观。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)