java 如果在 SAX 中设置 setNamespaceAware(true),如何获取“xmlns:XXX”属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5416637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get "xmlns:XXX" attribute if set setNamespaceAware(true) in SAX?
提问by DeepNightTwo
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)
.
设置后setNamespaceAware=true
,我无法获取方法xmlns:XXX
参数attributes
中的属性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.
我只是得到name
和targetNamespace
属性。xmlns
,xmlns:wsdl
,xmlns:mime
,xmlns:http
并且xmlns:tns
是在attributes
参数。但它们是不可访问的。
Is there any way to use setNamespaceAware=true
and get all attributes of a node?
有没有办法使用setNamespaceAware=true
和获取节点的所有属性?
回答by Joachim Sauer
When your XML parser is XML Namespace aware, then you should not needaccess to those properties, as they only define the short names for namespaces used in your XML.
当您的 XML 解析器能够识别 XML 命名空间时,您就不需要访问这些属性,因为它们只定义 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
).
在这种情况下,您总是使用它们的全名(例如http://schemas.xmlsoap.org/wsdl/
)来引用名称空间,并且可以忽略它们在 XML 中的别名(例如wsdl
)。
The fact that SAX doesn't provide those values is documented on the Attributes
class:
SAX 不提供这些值的事实记录在Attributes
类中:
It will [...] not contain attributes used as Namespace declarations (
xmlns*
) unless thehttp://xml.org/sax/features/namespace-prefixes
feature is set totrue
(it isfalse
by default).
xmlns*
除非该http://xml.org/sax/features/namespace-prefixes
功能设置为true
(false
默认情况下),否则它将 [...] 不包含用作命名空间声明 ( ) 的属性。
So using saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true)
should help you get to those values.
因此使用saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true)
应该可以帮助您获得这些值。
回答by bdoughan
The standard way to get the namespace declarations is from the startPrefixMapping event:
获取命名空间声明的标准方法是从 startPrefixMapping 事件: