Finds the text value of an element in the XML hierarchy specified by the list. @param list A list of strings representing the XML path. Each entry corresponds to an XML tag. To apply a text filter, use "tag~text" syntax (e.g., "name~FPC 0 CPU"). For example, to extract <temp
(List<String> list)
| 373 | * or if the element has no text content. |
| 374 | */ |
| 375 | public String findValue(List<String> list) { |
| 376 | if (list == null || list.isEmpty()) { |
| 377 | return null; |
| 378 | } |
| 379 | |
| 380 | Element nextElement = ownerDoc.getDocumentElement(); |
| 381 | boolean nextElementFound; |
| 382 | for (int k=0; k<list.size(); k++) { |
| 383 | nextElementFound = false; |
| 384 | String nextElementName = list.get(k); |
| 385 | if (!nextElementName.contains("~")) { |
| 386 | NodeList nextElementList = |
| 387 | nextElement != null |
| 388 | ? nextElement.getElementsByTagName(nextElementName) |
| 389 | : null; |
| 390 | if (nextElementList == null || nextElementList.getLength() == 0) { |
| 391 | logger.fine("Element '" + nextElementName + "' not found in findValue()"); |
| 392 | return null; |
| 393 | } |
| 394 | |
| 395 | /* If the next‑to‑next (n2n) element is a filter based on |
| 396 | * text value, then do the required filtering. |
| 397 | */ |
| 398 | String n2nString = null; |
| 399 | if (k<list.size()-1) |
| 400 | n2nString = list.get(k+1); |
| 401 | if (n2nString != null && n2nString.contains("~")) { |
| 402 | /* Since the n2n element is a filter based on text |
| 403 | * value( decided by '~') |
| 404 | * we now traverse the entire NodeList to find the |
| 405 | * correct nextElement |
| 406 | * based on the text value of the filter. |
| 407 | */ |
| 408 | String n2nText = n2nString.substring(n2nString. |
| 409 | indexOf("~") + 1); |
| 410 | String n2nElementName = n2nString.substring(0, |
| 411 | n2nString.indexOf("~")); |
| 412 | for (int i=0; i<nextElementList.getLength(); i++) { |
| 413 | nextElement = (Element)nextElementList.item(i); |
| 414 | NodeList nodes = nextElement.getElementsByTagName(n2nElementName); |
| 415 | if (nodes.getLength() == 0) continue; |
| 416 | Element n2nElement = (Element) nodes.item(0); |
| 417 | if (n2nElement == null || n2nElement.getFirstChild() == null) continue; |
| 418 | String text = trim(n2nElement.getFirstChild().getNodeValue()); |
| 419 | if (text.equals(n2nText)) { |
| 420 | nextElementFound = true; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | if (!nextElementFound) |
| 425 | return null; |
| 426 | } else { |
| 427 | nextElement = (Element)nextElementList.item(0); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | if (nextElement == null) { |
| 432 | return null; |