0

I want to use getChildText() to get text from a node that is a few levels deep. There are two namespaces in the file. The syntax below does not work and sets textToGet to null.

 String textToGet = root.getChildText("ns1:Customer/ns1:Address/ns1:Street/ns2:Streetname");

I know there is an alternative of first getting the Child Element, and then its Text, but I want to use a one-liner.

Also, would rather not chain getChild(), because some of the elements are not guaranteed to be in the file.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Kirill Yunussov
  • 1,955
  • 1
  • 21
  • 24

1 Answers1

1

You are not going to be able to make that a one-liner....

Consider using XPaths.... JDOM 2.x should help with that:

XPathExpression<String> xpe = XPathFactory.instance().compile(
    Filters.fstring(), "ns1:Customer/ns1:Address/ns1:Street/ns2:Streetname",
    null, namespace_ns1, namespace_ns2);
String textToGet = xpe.evaluateFirst(root);

(textToGet may be null)

Edit, the XPath expression above actually returns an element... you should add "/text()" to the end of the XPath, or change textToGet to be String (and the Filters too).

Rolf

rolfl
  • 17,539
  • 7
  • 42
  • 76