如何获取“xmlns:XXX”属性是否在 SAX 中设置 setNamespaceAware(true)?
这是我的代码:
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
后,我无法在方法的参数
。attributes
中获取xmlns:XXX
属性public void startElement(String uri, String localName, String qName, 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
和 targetNamespace
属性。 xmlns
、xmlns:wsdl
、xmlns:mime
、xmlns:http
和 xmlns:tns
code> 在 attributes
参数中。但它们是不可访问的。
有没有办法使用 setNamespaceAware=true 并获取节点的所有属性?
Here is my code:
path = wsdlPath;
SAXParserFactory saxfac = SAXParserFactory.newInstance();
saxfac.setNamespaceAware(true);
saxfac.setXIncludeAware(true);
saxfac.setValidating(false);
SAXParser saxParser = saxfac.newSAXParser();
saxParser.parse(wsdlPath, this);
After Setting setNamespaceAware=true
, I can't get the xmlns:XXX
attributes in parameter attributes
of method public void startElement(String uri, String localName, String qName, Attributes attributes)
.
for the following node:
<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/">
I just get name
and targetNamespace
attribute. xmlns
, xmlns:wsdl
, xmlns:mime
, xmlns:http
and xmlns:tns
are in the attributes
parameter. But they are not accessible.
Is there any way to use setNamespaceAware=true
and get all attributes of a node?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您的 XML 解析器支持 XML 命名空间时,您就不需要访问这些属性,因为它们只定义 XML 中使用的命名空间的短名称。
在这种情况下,您始终使用名称空间的全名(例如
http://schemas.xmlsoap.org/wsdl/
)来引用名称空间,并且可以忽略它们在 XML 中的别名(例如wsdl
)。SAX 不提供这些值的事实记录在
属性
类:因此使用 < code>saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true) 应该可以帮助您获取这些值。
When your XML parser is XML Namespace aware, then you should not need access to those properties, as they only define the short names for namespaces used in your XML.
In that case you always refer to the name spaces using their full name (e.g.
http://schemas.xmlsoap.org/wsdl/
) and can ignore what short name they are aliased to in the XML (e.g.wsdl
).The fact that SAX doesn't provide those values is documented on the
Attributes
class:So using
saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true)
should help you get to those values.获取命名空间声明的标准方法是从 startPrefixMapping 事件:
The standard way to get the namespace declarations is from the startPrefixMapping event: