开发者

How to get "xmlns:XXX" attribute if set setNamespaceAware(true) in SAX?

Here is my code:

path = wsdlPath;
S开发者_JS百科AXParserFactory 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?


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:

It will [...] not contain attributes used as Namespace declarations (xmlns*) unless the http://xml.org/sax/features/namespace-prefixes feature is set to true (it is false by default).

So using saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true) should help you get to those values.


The standard way to get the namespace declarations is from the startPrefixMapping event:

  • http://download.oracle.com/javase/6/docs/api/org/xml/sax/helpers/DefaultHandler.html#startPrefixMapping(java.lang.String,%20java.lang.String)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜