2013-08-30 74 views
0

我試圖使用DOM解析解析這個XML:DOM解析Java中2個問題

<Hospitals> 
    <Hospital> 
     <name>Hospital1</name> 
     <tel-number>0272222222</tel-number> 
    </Hospital> 
    <Hospital> 
     <name>Hospital2</name> 
     <tel-number>0273333333</tel-number> 
    </Hospital> 
    <Hospital> 
     <name>Hospital3</name> 
     <tel-number>0270000000</tel-number> 
    </Hospital> 
</Hospitals> 

而且我使用的測試驗證碼,只想得到第一醫院的名字:

public void viewXmlData(Document doc) { 
    Node na = doc.getElementsByTagName("Hospital").item(0); 

    NodeList nList1 = na.getChildNodes(); 

    int index = getNodeIndex(nList1, "name"); 

    Node nb = nList1.item(index); 

    NodeList nList2 = nb.getChildNodes(); 

    String hospitalName = nList2.item(0).getTextContent(); 

    Toast.makeText(MainActivity.this, hospitalName, Toast.LENGTH_LONG) 
      .show(); 
} 

private int getNodeIndex(NodeList nList, String nodeName) { 
    for (int i = 0; i < nList.getLength(); i++) { 
     if (nList.item(i).getNodeName().equals(nodeName)) 
      return i; 
    } 

    return -1; 
} 

工作正常。但我有兩個問題:

1-爲什麼index等於1不是0,那麼第一個節點是什麼呢?

2-爲什麼我要創建nList2?我以爲我達到了確切的節點:Node nb = nList1.item(index);

我知道使用Element類將使事情變得更簡單,但我仍然需要知道答案。

+1

也許第一節點(索引0)是''和''之間的空格。 –

回答

1

1-正如我所說,在節點0 ist的空白<Hospital><name之間。

2 - 您不必創建nList2您可以直接與節點 nb調用getTextContent

package com.example.tests.xml; 

import java.io.StringReader; 

import javax.xml.parsers.DocumentBuilderFactory; 

import junit.framework.Assert; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

public class XmlDomTest 
{ 
    private Document _target; 

    @Before 
    public void setUp() { 
     final String xmlString = new String(
     "<Hospitals>\n" + 
     " <Hospital>\n" + 
     "  <name>Hospital1</name>\n" + 
     "  <tel-number>0272222222</tel-number>\n" + 
     " </Hospital>\n" + 
     " <Hospital>\n" + 
     "  <name>Hospital2</name>\n" + 
     "  <tel-number>0273333333</tel-number>\n" + 
     " </Hospital>\n" + 
     " <Hospital>\n" + 
     "  <name>Hospital3</name>\n" + 
     "  <tel-number>0270000000</tel-number>\n" + 
     " </Hospital>\n" + 
     "</Hospitals>" 
    ); 
     try (// autoclose: 
     StringReader reader = new StringReader(xmlString); 
    ) { 
     this._target = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(reader)); 
     } catch(final Exception e) { 
     Assert.fail(e.getMessage()); 
     } 
    } 

    @After 
    public void tearDown() { 
     this._target = null; 
    } 

    @Test 
    public void testReadDom(
    ) { 
     final Node na = this._target.getElementsByTagName("Hospital").item(0); 

     final NodeList nList1 = na.getChildNodes(); 

     final int index = this.getNodeIndex(nList1, "name"); 

     final Node nb = nList1.item(index); 

     Assert.assertEquals("\n  ", nList1.item(0).getTextContent()); 

     final String hospitalName = nb.getTextContent(); 

     Assert.assertEquals("Hospital1", hospitalName); 
    } 

    private int getNodeIndex(final NodeList nList, final String nodeName) { 
     for (int i = 0; i < nList.getLength(); i++) { 
      if (nList.item(i).getNodeName().equals(nodeName)) { 
      return i; 
     } 
     } 

     return -1; 
    } 
} 
+0

我檢查過,沒錯,我可以直接使用nb。謝謝。 –