2015-09-14 31 views
0

我想將xml字符串數據轉換爲NodeList並迭代..但是,我無法。我想迭代節點列表並獲取其中的XML數據。這是我的代碼不能夠使用Java和迭代將字符串xml轉換爲節點

String arrayOfErrorContext = "<item><errorCode>1</errorCode><errorDescription></errorDescription></item>" + 
      "<item><errorCode>1</errorCode><errorDescription></errorDescription></item>"; 
    if(arrayOfErrorContext!= null && !arrayOfErrorContext.isEmpty()) { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     try 
     { 
      builder = factory.newDocumentBuilder(); 
      Document document = builder.parse(new InputSource(new StringReader(arrayOfErrorContext))); 
      NodeList nList1 = document.getElementsByTagName("item"); 
      //iteration Logic on nList1 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

上面的代碼中有任何錯誤?

+0

@wero,所以什麼毛病然後 – Syed

回答

0

首先糾正XML字符串。 pu根元素像..

你正在做錯誤的方式.. 你需要將字符串轉換爲dom.Document。然後嘗試從中獲取NodeList。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     try 
     { 
      builder = factory.newDocumentBuilder(); 
      Document document = builder.parse(new InputSource(new StringReader(arrayOfErrorContext))); 
      NodeList nList1 = document.getElementsByTagName("item"); 
      //iteration Logic on nList1 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

在該迭代邏輯得到一個節點

nList1.item(0).getFirstChild() 

,並通過使用讀取下一個節點進行迭代。

.getNextSibling() 

示例迭代邏輯:

public static void recurseNode(Node node) { 
     System.out.println(node.getNodeName() + node.getTextContent()); 

     NodeList nodeList = node.getChildNodes(); 
     for (int i = 0; i < nodeList.getLength(); i++) { 
      Node currentNode = nodeList.item(i); 
      if (currentNode.getNodeType() == Node.ELEMENT_NODE) { 
       // calls this method for all the children which is Element 
       recurseNode(currentNode); 
      } 
     } 

完整代碼:

import java.io.StringReader; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.Document; 

import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

public class EnterExitListener { 

    static Node e = null; 

    public static void main(String[] argd) { 
     String arrayOfErrorContext = "<root><item><errorCode>1</errorCode><errorDescription></errorDescription></item><item><errorCode>1</errorCode><errorDescription></errorDescription></item></root>"; 

     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder; 
     try { 
      builder = factory.newDocumentBuilder(); 
      Document document = builder.parse(new InputSource(new StringReader(
        arrayOfErrorContext))); 
      NodeList nList1 = document.getElementsByTagName("root"); 
      int i = nList1.getLength(); 

      e = nList1.item(0).getFirstChild(); 
      e.getChildNodes(); 
      recurseNode(e); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void recurseNode(Node node) { 
     System.out.println(node.getNodeName() + node.getTextContent()); 

     NodeList nodeList = node.getChildNodes(); 
     for (int i = 0; i < nodeList.getLength(); i++) { 
      Node currentNode = nodeList.item(i); 
      if (currentNode.getNodeType() == Node.ELEMENT_NODE) { 
       // calls this method for all the children which is Element 
       recurseNode(currentNode); 
      } 
     } 

    } 
} 
+0

號對不起,它示出了[致命錯誤】:1:76 :根元素之後的文檔中的標記必須是格式良好的。 – Syed

+0

我已更新我的問題。你可以請檢查它 – Syed

+0

我已編輯的答案希望它可以幫助你。還有你提供的XML字符串,它包含一些問題糾正它 –