2012-10-31 50 views
6

我想要得到的數值超出TAG2和我得到這個一個xml:爲什麼在java中getElementsByTagNameNS爲空?

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
      "<ns1:schema xmlns:ns1='http://example.com'>" + 
       "<ns1:tag1>" + 
       " <ns1:tag2>value</ns1:tag2>" + 
       "</ns1:tag1>" + 
      "</ns1:schema>"; 

它然後解析到一個文件,並希望得到由tagnameNS的元素。但是,當我運行這個節點列表是空的,爲什麼?

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
     docBuilderFactory.setNamespaceAware(true); 
     DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
     Document doc = docBuilder.parse(new InputSource(new StringReader(xml))); 

     NodeList nl = doc.getElementsByTagNameNS("http://example.com", "tag2"); 

     String a = nl.item(0).getNodeValue(); 

仍然不能使用URI。

+3

無關,但爲了減少全世界編碼相關的問題,我建議你用'new InputSource(new StringReader(xml))'替換'new ByteArrayInputStream(xml.getBytes())''。 – Isaac

回答

15

getElementsByTagNameNS正在迴應一個結果。問題在於你目前正在調用錯誤的方法從結果元素中獲取文本內容。你需要調用getTextContext()而不是getNodeValue()

String a = nl.item(0).getTextContent(); 

DomDemo

下面是一個完整的代碼示例。

package forum13166195; 

import java.io.StringReader; 
import javax.xml.parsers.*; 
import org.w3c.dom.*; 
import org.xml.sax.InputSource; 

public class DomDemo { 

    public static void main(String[] args) throws Exception{ 
     String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
        + "<ns1:schema xmlns:ns1='http://example.com'>" 
         + "<ns1:tag1>" 
          + "<ns1:tag2>value</ns1:tag2>" 
         + "</ns1:tag1>" 
        + "</ns1:schema>"; 

     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
     docBuilderFactory.setNamespaceAware(true); 
     DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
     Document doc = docBuilder.parse(new InputSource(new StringReader(xml))); 

     NodeList nl = doc.getElementsByTagNameNS("http://example.com", "tag2"); 

     String a = nl.item(0).getTextContent(); 
     System.out.println(a); 
    } 

} 

輸出

value 

替代方法

也可以使用javax.xml.xpath的API(包含在Java SE 5及以上)來查詢從一個值XML文檔。這些API提供比getElementsByTagNameNS更多的控制。

XPathDemo

package forum13166195; 

import java.io.StringReader; 
import java.util.Iterator; 
import javax.xml.namespace.NamespaceContext; 
import javax.xml.xpath.*; 

import org.xml.sax.InputSource; 

public class XPathDemo { 

    public static void main(String[] args) throws Exception{ 
     String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 
        + "<ns1:schema xmlns:ns1='http://example.com'>" 
         + "<ns1:tag1>" 
          + "<ns1:tag2>value</ns1:tag2>" 
         + "</ns1:tag1>" 
        + "</ns1:schema>"; 

     XPath xpath = XPathFactory.newInstance().newXPath(); 
     xpath.setNamespaceContext(new NamespaceContext() { 

      public String getNamespaceURI(String arg0) { 
       if("a".equals(arg0)) { 
        return "http://example.com"; 
       } 
       return null; 
      } 

      public String getPrefix(String arg0) { 
       return null; 
      } 

      public Iterator getPrefixes(String arg0) { 
       return null; 
      } 

     }); 

     InputSource inputSource = new InputSource(new StringReader(xml)); 
     String result = (String) xpath.evaluate("/a:schema/a:tag1/a:tag2", inputSource, XPathConstants.STRING); 
     System.out.println(result); 
    } 

} 

輸出

value 
+1

謝謝男人它的作品:) – user1512895

0

您需要傳遞名稱空間URL;而不是您在XML中創建的本地別名。

0
Try this : 

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
    docBuilderFactory.setNamespaceAware(true); 
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
    Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));  
    NodeList nodeList = doc.getElementsByTagNameNS("*", "tag2"); 
    String a = nodeList.item(0).getTextContent(); 
    System.out.println(a); 

輸出

value