1

I've written some code to extract different parts of from an XML string using xpath. The question I have is if the xml string was for example

<event>alarm, red, 2</event>

is there any easy way with xpath to select say the second word in the string after the first comma so "red"?? right now i am using

String event = xpath.evaluate("/event", doc);

which returns "alarm, red, 2" but all i want is "red". I know that i could just then take event and use substring to extract "red" but i am wondering if there is a way to do this using xpath rather than substring?

code:

String xml = "<event>alarm, red, 2<event>";  

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();

InputSource source = new InputSource(new StringReader(xml));
Document doc = (Document) xpath.evaluate ("/", source, XPathConstants.NODE);
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69

2 Answers2

2

If you use an XPath 2.0 implementation, you can use tokenize and use a regular expression to split the string by the comma and then select the second token:

tokenize(/event, ',')[2]
helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • awesome! how would I implement this into my code? something like String event = xpath.evaluate("/event", doc).tokenize(/event, '2')[2];?? – Paul Alexander Mar 31 '14 at 14:05
  • `tokenize` is XPath 2.0, so you need an implementation which supports it such as Saxon 9. See [this question](http://stackoverflow.com/questions/926222/using-saxon-xpath-engine-in-java) about setting up Saxon in Java. – helderdarocha Mar 31 '14 at 14:13
1

I don't think it's possible & it's probably not a good idea to do that, content of your element is supposed to be atomic, I would avoid mixing this parsing with Xpath and simply wrap the event with a utility method garbing the first word.

gjambet
  • 369
  • 5
  • 13