在 Android 中搜索从 XML 解析的数组

发布于 2024-10-05 09:37:23 字数 1802 浏览 1 评论 0原文

我有一个 XML 文件,我将其解析为一个 ArrayList

在这个 ArrayList 中,我有国家/地区以及其中国家/地区的警报号码。

我想搜索一个国家并找到警察、救护车或消防车。数字。

这是可以帮助您的代码。

将 XML 解析为 ArrayList:

protected ArrayList<Alarmdiensten> getAlarmdiensten() {
     ArrayList<Alarmdiensten> lijst = new ArrayList<Alarmdiensten>();
     try {
      DocumentBuilder builder =DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = builder.parse(getAssets().open("alarmdiensten.xml"));
      NodeList nl = doc.getElementsByTagName("land");
      for (int i=0;i<nl.getLength();i++) {
       Node node = nl.item(i);
       Alarmdiensten land = new Alarmdiensten();

                land.land = Xml.innerHtml(Xml.getChildByTagName(node, "naam"));
       land.landcode = Xml.innerHtml(Xml.getChildByTagName(node, "code"));
       land.politie = Xml.innerHtml(Xml.getChildByTagName(node, "politie"));
       land.ambulance = Xml.innerHtml(Xml.getChildByTagName(node, "ambulance"));
       land.brandweer = Xml.innerHtml(Xml.getChildByTagName(node, "brandweer"));
       land.telamba = Xml.innerHtml(Xml.getChildByTagName(node, "telamba"));
       land.adresamba = Xml.innerHtml(Xml.getChildByTagName(node, "adresamba"));

       lijst.add(land);
      }
     } catch (Exception e) {;
     }
     return lijst;
    }

将使用警报号码的方法:

    public void AlarmMenu(){
     String landcode;
        ArrayList<Alarmdiensten> diensten = getAlarmdiensten();
     if(fakelocation = true) {
      landcode = sfakelocation;
     }
     else {
      try {
    landcode = getAddressForLocation(this, locationNow).getCountryCode();
   } catch (IOException e) {
    e.printStackTrace();
   }
     }

所以我有土地代码,我想在 ArrayList 中搜索属于土地代码的数字。

我该怎么做?

I have this XML file which I parse into an ArrayList

In this ArrayList I have countries and the alarmnumbers for the countries in it.

I want to search to a country and get it's police, ambulance or firedep. number.

Here is the code to help you out.

Parsing XML into ArrayList:

protected ArrayList<Alarmdiensten> getAlarmdiensten() {
     ArrayList<Alarmdiensten> lijst = new ArrayList<Alarmdiensten>();
     try {
      DocumentBuilder builder =DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = builder.parse(getAssets().open("alarmdiensten.xml"));
      NodeList nl = doc.getElementsByTagName("land");
      for (int i=0;i<nl.getLength();i++) {
       Node node = nl.item(i);
       Alarmdiensten land = new Alarmdiensten();

                land.land = Xml.innerHtml(Xml.getChildByTagName(node, "naam"));
       land.landcode = Xml.innerHtml(Xml.getChildByTagName(node, "code"));
       land.politie = Xml.innerHtml(Xml.getChildByTagName(node, "politie"));
       land.ambulance = Xml.innerHtml(Xml.getChildByTagName(node, "ambulance"));
       land.brandweer = Xml.innerHtml(Xml.getChildByTagName(node, "brandweer"));
       land.telamba = Xml.innerHtml(Xml.getChildByTagName(node, "telamba"));
       land.adresamba = Xml.innerHtml(Xml.getChildByTagName(node, "adresamba"));

       lijst.add(land);
      }
     } catch (Exception e) {;
     }
     return lijst;
    }

The method that will use the alarmnumbers:

    public void AlarmMenu(){
     String landcode;
        ArrayList<Alarmdiensten> diensten = getAlarmdiensten();
     if(fakelocation = true) {
      landcode = sfakelocation;
     }
     else {
      try {
    landcode = getAddressForLocation(this, locationNow).getCountryCode();
   } catch (IOException e) {
    e.printStackTrace();
   }
     }

So I have the landcode, and I want to search in the ArrayList diensten for the numbers that belong with the landcode.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗心人 2024-10-12 09:37:23

现在你已经有了一个 Alarmdiensten 对象的 ArrayList。我建议您可能希望将其更改为地图,以便存储土地代码与 Alarmdiensten 对象的地图。

这样,您就可以使用土地代码从地图中获取 Alarmdiensten,然后只需在 Alarmdiensten 对象上调用 getPolitie() 等方法即可。

我会确保你封装你的 Alarmdiensten 对象顺便说一句,直接访问它的私有成员是有点禁忌:)

所以像这样:

protected Map<String, Alarmdiensten> getAlarmdiensten()
{
  Map<String, Alarmdiensten> alarmNumbersForCountries 
                                             = new HashMap<String, Alarmdiensten>();

  try
  {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(getAssets().open("alarmdiensten.xml"));
    NodeList nl = doc.getElementsByTagName("land");
    for (int i = 0; i < nl.getLength(); i++)
    {
      Node node = nl.item(i);
      Alarmdiensten land = new Alarmdiensten();

      land.setLand(Xml.innerHtml(Xml.getChildByTagName(node, "naam")));
      land.setLandcode(Xml.innerHtml(Xml.getChildByTagName(node, "code")));
      land.setPolitie(Xml.innerHtml(Xml.getChildByTagName(node, "politie")));
      land.setAmbulance(Xml.innerHtml(Xml.getChildByTagName(node, "ambulance")));
      land.setBrandweer(Xml.innerHtml(Xml.getChildByTagName(node, "brandweer")));
      land.setTelamba(Xml.innerHtml(Xml.getChildByTagName(node, "telamba")));
      land.setAdresamba(Xml.innerHtml(Xml.getChildByTagName(node, "adresamba")));

      alarmNumbersForCountries.put(land.getLandCode(), land);
    }
  } 
  catch (Exception e)
  {
    // Handle Exception
  } 
  return alarmNumbersForCountries;
}

从 Map 中获取条目

Alarmdiensten land = alarmNumbersForCountries.get(landcode);

另一个 YMMV 点是你可能想要拆分出来方法中通过 XML 解析构建 Alarmdiensten 对象的部分。 “每种方法都应该做一件事,做好一件事。”

Well at the moment you've got an ArrayList of Alarmdiensten objects. I would suggest you might want to change that to a Map so that you are storing a Map of land codes vs your Alarmdiensten objects.

That way you get the Alarmdiensten out of the Map using the landcode and then simply call the getPolitie() etc methods on your Alarmdiensten object.

I would make sure that you encapsulate your Alarmdiensten object BTW, accessing it's private members directly is a bit of a no-no :)

So something like:

protected Map<String, Alarmdiensten> getAlarmdiensten()
{
  Map<String, Alarmdiensten> alarmNumbersForCountries 
                                             = new HashMap<String, Alarmdiensten>();

  try
  {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(getAssets().open("alarmdiensten.xml"));
    NodeList nl = doc.getElementsByTagName("land");
    for (int i = 0; i < nl.getLength(); i++)
    {
      Node node = nl.item(i);
      Alarmdiensten land = new Alarmdiensten();

      land.setLand(Xml.innerHtml(Xml.getChildByTagName(node, "naam")));
      land.setLandcode(Xml.innerHtml(Xml.getChildByTagName(node, "code")));
      land.setPolitie(Xml.innerHtml(Xml.getChildByTagName(node, "politie")));
      land.setAmbulance(Xml.innerHtml(Xml.getChildByTagName(node, "ambulance")));
      land.setBrandweer(Xml.innerHtml(Xml.getChildByTagName(node, "brandweer")));
      land.setTelamba(Xml.innerHtml(Xml.getChildByTagName(node, "telamba")));
      land.setAdresamba(Xml.innerHtml(Xml.getChildByTagName(node, "adresamba")));

      alarmNumbersForCountries.put(land.getLandCode(), land);
    }
  } 
  catch (Exception e)
  {
    // Handle Exception
  } 
  return alarmNumbersForCountries;
}

To get the entry out of the Map

Alarmdiensten land = alarmNumbersForCountries.get(landcode);

Another YMMV point is that you might want to split out the part of your method that builds Alarmdiensten objects from the XML parsing. "Each method should do one thing and one thign well."

酒与心事 2024-10-12 09:37:23

使用 for 循环并搜索它

for(Alarmdiensten land :diensten){
        if(land.landcode.equals(landcode) ){
           // yes i got it, The current land. 
            break;
        }
    }

Use a for loop and search for it

for(Alarmdiensten land :diensten){
        if(land.landcode.equals(landcode) ){
           // yes i got it, The current land. 
            break;
        }
    }
深海不蓝 2024-10-12 09:37:23

只需迭代列表:

String landcode = getLandCode();
for (Alarmdiensten dienst:diensten) {
  if (dienst.landcode.equals(landcode)) {
    // do what has to be done
  }
}

如果您必须更频繁地查找值,请考虑使用映射而不是列表:

Map<String, List<Alarmdiensten>> servicesInCountry 
                   = new HashMap<String, List<Alarmdiensten>>();
for (Alarmdiensten dienst:diensten) {
  List<Alarmdiensten> list = servicesInCountry.get(dienst.landcode);
  if (list == null) {
     list = new ArrayList<Alarmdiensten>();
     servicesInCountry.put(dienst.landcode, list);
  }
  list.add(dienst);
}

// ... and later on
List<Alarmdiensten> servicesInSweden = servicesInCountry.get("SWE");

Just iterate over the list:

String landcode = getLandCode();
for (Alarmdiensten dienst:diensten) {
  if (dienst.landcode.equals(landcode)) {
    // do what has to be done
  }
}

Consider using a map instead of a list, if you have to lookup the values more frequently:

Map<String, List<Alarmdiensten>> servicesInCountry 
                   = new HashMap<String, List<Alarmdiensten>>();
for (Alarmdiensten dienst:diensten) {
  List<Alarmdiensten> list = servicesInCountry.get(dienst.landcode);
  if (list == null) {
     list = new ArrayList<Alarmdiensten>();
     servicesInCountry.put(dienst.landcode, list);
  }
  list.add(dienst);
}

// ... and later on
List<Alarmdiensten> servicesInSweden = servicesInCountry.get("SWE");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文