简体   繁体   中英

Selecting multiple nodes using xpath in java

I an trying to get data using XPath.

I can reach the data what I want, but when there are multiple data, only the first one is selected. And I want to count the number of target data.

For example, I want to count the numbers of queues whose message-vpn's name is vpn/b. XML structure is as follows:

<queues>
    <queue>
        <name> queue/a </name>
        <info>
            <message-vpn> vpn/a </message-vpn>
        </info>
    </queue>
    <queue>
        <name> queue/b </name>
        <info>
            <message-vpn> vpn/b </message-vpn>
        </info>
    </queue>
    <queue>
        <name> queue/c </name>
        <info>
            <message-vpn> vpn/b </message-vpn>
        </info>
    </queue>
</queues>

And here's the xpath script I used.

/queues/queue/info/message-vpn[text()=("vpn/b")]

When I access the data, only queue/b is selected, not c. Please help me to do that..

Try below:

//queues/queue/info/message-vpn[contains(.,'vpn/b')]

Your original xpath expression fails because the text vpn/b is surrounded by spaces. So another way to do it is

/queues/queue/info/message-vpn[normalize-space()=("vpn/b")]

If you want to count the number of these queues, use the count() method:

count(/queues/queue/info/message-vpn[normalize-space()=("vpn/b")])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM