2013-05-17 3 views
8

का उपयोग कर इस इनपुट XML है नामस्थान उपसर्ग के साथ एक्सएमएल पढ़ने में असमर्थ:डोम पार्सर

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Header/> 
    <SOAP-ENV:Body> 
     <ns2:SendResponse xmlns:ns2="http://mycompany.com/schema/"> 
     <ns2:SendResult> 
      <ns2:Token>A00179-02</ns2:Token> 
     </ns2:SendResult> 
     </ns2:SendResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

इस कोड है कि मैं एक्सएमएल को पढ़ने के लिए उपयोग कर रहा हूँ (वैरिएबल xmlString एक्सएमएल ऊपर होता है):

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
dbf.setNamespaceAware(true); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
InputSource is = new InputSource(); 
is.setCharacterStream(new StringReader(xmlString)); 
Document doc = db.parse(is); 

System.out.println("Element :" + doc.getElementsByTagName("Token").item(0)); 
System.out.println("Element :" + doc.getElementsByTagName("ns2:Token").item(0)); 

आउटपुट:

Element :null 
Element :[ns2:Token: null] 

मैं अगर मैं का उपयोग तत्व को पढ़ने में सक्षम हूँ "ns2: टोकन" टैग नाम के रूप में है, लेकिन मैं वें का उपयोग नहीं करना चाहते हैं ई मेरे कोड में उपसर्ग क्योंकि मुझे यकीन नहीं है कि यह भविष्य में समान होगा या बदल जाएगा। क्या टैग नाम में नेमस्पेस को हार्ड-कोडिंग किए बिना xml तत्व को पढ़ने का कोई तरीका है?

उत्तर

8

namespaced तत्वों के लिए W3C dom विधि: तो मैं उसका उपयोग नहीं करते

getElementsByTagNameNS 

NodeList getElementsByTagNameNS(String namespaceURI, 
           String localName) 

    Returns a NodeList of all the Elements with a given local name and namespace URI in document order. 

    Parameters: 
     namespaceURI - The namespace URI of the elements to match on. The special value "*" matches all namespaces. 
     localName - The local name of the elements to match on. The special value "*" matches all local names. 
    Returns: 
     A new NodeList object containing all the matched Elements. 
    Since: 
     DOM Level 2 

IIRC पहले W3C डोम के संस्करण नामस्थान के लिए गरीब समर्थन प्राप्त था। हालांकि यदि आप उपरोक्त का उपयोग पूर्ण नेमस्पेसुरि http://schemas.xmlsoap.org/soap/envelope/ के साथ करते हैं तो इसे काम करना चाहिए। उपसर्ग महत्वहीन है - यह दस्तावेज़ के बाहर कोई स्थायित्व है उस में प्रयोग किया जाता है

इसलिए कोशिश: XPath अभिव्यक्ति का उपयोग करने के

System.out.println("Element :" + doc.getElementsByTagNameNS(
     "http://schemas.xmlsoap.org/soap/envelope/", "Token").item(0)); 
0

आप हमेशा एक चर के लिए नामस्थान असाइन कर सकते हैं, इससे भविष्य में इसे उड़ाने की अनुमति मिल जाएगी।

0

प्रयास करें।। नीचे नमूना कोड देखें।

Document doc = dBuilder.parse(new ByteArrayInputStream(responseXML.getBytes())); 
doc.getDocumentElement().normalize(); 
XPath xPath = XPathFactory.newInstance().newXPath(); 

String expression = "/ns6:ReadPersonReturn/ns6:object/ns3:Person/ns3:Phone/ns3:item"; 
NodeList nodes = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); 
Element secondNode = null; 
if(nodes != null && nodes.getLength() > 0){ 
    secondNode = (Element) leadCloudPingRecordNodes.item(i); 
} 
1

नाम स्थान पहले

docFactory.setNamespaceAware(true); 
StringBuilder nameSpace = new StringBuilder(
        doc.getDocumentElement().getPrefix() != null ? doc.getDocumentElement().getPrefix() + ":" : ""); 

तो नाम स्थान चर का उपयोग मिलता है accrodingly

जैसे:

Node node= doc.getElementsByTagName(nameSpace + "Node1").item(0) 
        .getFirstChild(); 
+0

हो सकता है कि एक हैक है, लेकिन एक आकर्षण की तरह मेरी समस्या हल करती है :) – Tarmo

संबंधित मुद्दे