0

我正在尝试使用 DOM 解析来解析这个 XML:

<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 类会使事情变得更简单,但我仍然需要知道答案。

4

1 回答 1

1

<Hospital>1-正如我所说,在节点 0 中是和之间的空格<name

2-您不必创建nList2可以getTextContent直接使用节点 调用nb

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;
  }
}
于 2013-08-30T07:04:21.510 回答