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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:00:38  来源:igfitidea点击:

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

javaxmlnamespacessaxparser

提问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:XXXattributes in parameter attributesof 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 nameand targetNamespaceattribute. xmlns, xmlns:wsdl, xmlns:mime, xmlns:httpand xmlns:tnsare in the attributesparameter. But they are not accessible.

我只是得到nametargetNamespace属性。xmlnsxmlns:wsdlxmlns:mimexmlns:http并且xmlns:tns是在attributes参数。但它们是不可访问的。

Is there any way to use setNamespaceAware=trueand 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 Attributesclass:

SAX 不提供这些值的事实记录在Attributes类中

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

xmlns*除非该http://xml.org/sax/features/namespace-prefixes功能设置为truefalse默认情况下),否则它将 [...] 不包含用作命名空间声明 ( ) 的属性。

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)应该可以帮助您获得这些值。