2011-03-24 90 views
5

這裏是我的代碼:如果在SAX中設置setNamespaceAware(true),如何獲得「xmlns:XXX」屬性?

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)的參數attributesxmlns:XXX屬性。

以下節點:

<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/"> 

我只是得到nametargetNamespace屬性。 xmlns,xmlns:wsdl,xmlns:mime,xmlns:httpxmlns:tnsattributes參數中。但他們無法訪問。

有什麼方法可以使用setNamespaceAware=true並獲得節點的所有屬性?

回答

7

如果您的XML解析器是XML名稱空間感知的,那麼您不應該需要訪問這些屬性,因爲它們只定義XML中使用的名稱空間的短名稱。

在這種情況下,您總是使用全名引用名稱空間(例如http://schemas.xmlsoap.org/wsdl/),並且可以忽略在XML中別名的短名稱(例如wsdl)。

該SAX不提供這些值的事實被記錄在Attributes class

它將[...]不含有用作命名空間聲明(xmlns*)屬性,除非http://xml.org/sax/features/namespace-prefixes功能設置爲true(默認爲false)。

因此使用saxfac.setFeature("http://xml.org/sax/features/namespace-prefixes", true)應該可以幫助您獲得這些值。

+0

它的作品!許多許多感謝你! – DeepNightTwo 2011-03-24 09:24:23

+0

我嘗試使用這個建議,發現我也必須做以下操作:'saxfac.setFeature(「http://xml.org/sax/features/namespaces」,false);' – 2011-04-06 17:05:57