问题描述
这是我的代码:
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)
的参数attributes
中获得xmlns:XXX
属性.
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)
.
对于以下节点:
<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
在attributes
参数中.但是它们不可访问.
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.
是否可以使用setNamespaceAware=true
并获取节点的所有属性?
Is there any way to use setNamespaceAware=true
and get all attributes of a node?
推荐答案
当您的XML解析器支持XML命名空间时,您就不必访问这些属性,因为它们仅定义了短名称用于XML中使用的名称空间.
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.
在这种情况下,您始终使用其全名(例如http://schemas.xmlsoap.org/wsdl/
)来引用名称空间,并且可以忽略它们在XML中别名的短名称(例如wsdl
).
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
).
因此使用应该可以帮助您获得这些值.
So using saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true)
should help you get to those values.
这篇关于如何获取"xmlns:XXX"属性是否在SAX中设置setNamespaceAware(true)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!