A simple answer needed to a simple question.
For example:
String xml = "toyota";
String xpath = "/car/manufacturer";
assertEquals("toyota",evaluate(xml, xpath));
How can I write the evaluate method in simple and readable way that will work for any given well-formed xml and xpath.
Obviously there are loads of ways this can be achieved but the majority seem very verbose.
Any simple ways I'm missing/libraries that can achieve this?
For cases where multiple nodes are returned I just want the string representation of this.
解决方案
Here you go, the following can be done with Java SE:
import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;
public class Demo {
public static void main(String[] args) throws Exception {
String xml = "toyota";
String xpath = "/car/manufacturer";
XPath xPath = XPathFactory.newInstance().newXPath();
assertEquals("toyota",xPath.evaluate(xpath, new InputSource(new StringReader(xml))));
}
}