首页 > java > 如何使用Xpath(或使用什么)来获取来自不同父节点的不同节点子节点?

如何使用Xpath(或使用什么)来获取来自不同父节点的不同节点子节点? (How to get different node children from different parents using Xpath (or what to use instead)?)

问题

我正在使用的XML文件是一个在线strem,结构如下:
编辑:

<forecasts>
    <forecast  date="yyyy-mm-dd">
        <night>
            <place>
               <name>Chicago</name>
                 <tempmin>9</tempmin>
            </place>
            <place>
                <name>Denver</name>
                <tempmin>11</tempmin>
            </place>
        </night>
        <day>
            <place>
                <name>Chicago</name>
                <tempmax>19</tempmax>
            </place>
            <place>
                <name>Denver</name>
                <tempmax>20</tempmax>
            </place>
        </day>
    </forecast>
</forecasts>


所以在主要活动中有一个ListView,它显示所有地方(城市)的名称

当用户做出选择时它应该做什么:

  • 开始一个新的活动,程序将在其中
  • 显示地方的名称
  • 显示用户选择的tempmintempmax

现在,我无法弄清楚的是,如何获得丹佛的tempmin和tempmax值(例如)并将它们显示给用户。下面是列出所有地方的代码段

ArrayList<String> mPlaces = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        getPlaceList();
    } catch(Exception ex) {
        Toast.makeText(this, "Exception: " + ex.getMessage(), Toast.LENGTH_LONG).show();
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, mPlaces);
        setListAdapter(adapter);
        ListView lv = getListView();

    private void getPlaceList() throws Exception {
    URL url = new URL("http://www.this.url/thisurl/xml/forecast.php");
        InputSource inputSrc = new InputSource(url.openStream());
        // query XPath instance, this is the parser
        XPath xpath = XPathFactory.newInstance().newXPath();

        String names = "//night/place/name";
        NodeList nameList = (NodeList)xpath.evaluate(names, inputSrc, XPathConstants.NODESET);
        if(nameList != null && nameList.getLength() > 0) {
        mPlaces.clear();
        int len = nameList.getLength();

        for(int i = 0; i < len; ++i) {
            // query value
            Node node = nameList.item(i);
            mPlaces.add(node.getTextContent());
        }
    }
}

解决方法

如果我正确地理解了您的要求,您希望在<day/><night/>元素处获得某个位置。让我们假设您希望<place/>通过使用以下XPath 来获取第二个元素:

/forecast/(day, night)/place[2]

如果您有更多的父元素,并且您只想要所有这些元素,那么您也可以这样做

/forecast/*/place[2]

如果您知道所选名称而不是位置,则可以使用它来选择place元素:

/forecast/(day, night)/place[name = "Place 2"]

问题

The XML file I am working with is an online strem and is structured like this:
Edited:

<forecasts>
    <forecast  date="yyyy-mm-dd">
        <night>
            <place>
               <name>Chicago</name>
                 <tempmin>9</tempmin>
            </place>
            <place>
                <name>Denver</name>
                <tempmin>11</tempmin>
            </place>
        </night>
        <day>
            <place>
                <name>Chicago</name>
                <tempmax>19</tempmax>
            </place>
            <place>
                <name>Denver</name>
                <tempmax>20</tempmax>
            </place>
        </day>
    </forecast>
</forecasts>


So in the main activity there's a ListView, which is to display the names of all the places (cities).

What it's supposed to do when the user has made a selection:

  • start a new activity in which the program will
  • show the name of the place
  • show the tempmin and tempmax values of the user made selection

Now, what I cannot figure out, is how to get, the tempmin and tempmax values of Denver (for instance) and show them both to user. Below is the code snippet to list all the places

ArrayList<String> mPlaces = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        getPlaceList();
    } catch(Exception ex) {
        Toast.makeText(this, "Exception: " + ex.getMessage(), Toast.LENGTH_LONG).show();
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, mPlaces);
        setListAdapter(adapter);
        ListView lv = getListView();

    private void getPlaceList() throws Exception {
    URL url = new URL("http://www.this.url/thisurl/xml/forecast.php");
        InputSource inputSrc = new InputSource(url.openStream());
        // query XPath instance, this is the parser
        XPath xpath = XPathFactory.newInstance().newXPath();

        String names = "//night/place/name";
        NodeList nameList = (NodeList)xpath.evaluate(names, inputSrc, XPathConstants.NODESET);
        if(nameList != null && nameList.getLength() > 0) {
        mPlaces.clear();
        int len = nameList.getLength();

        for(int i = 0; i < len; ++i) {
            // query value
            Node node = nameList.item(i);
            mPlaces.add(node.getTextContent());
        }
    }
}

解决方法

If I understood your requirement correctly you want to get a certain place at either the <day/> or <night/> element. Lets assume you want to get the second <place/> element you can do so by using the following XPath:

/forecast/(day, night)/place[2]

If you have more parent elements and you want simply all of them you could also do

/forecast/*/place[2]

If you know the selected name instead of the position you can use it to select the place element:

/forecast/(day, night)/place[name = "Place 2"]
相似信息