App下載
首頁javaxpathJava HTML/XML - 如何評估字符串上的xpath并返回結(jié)果字符串

Java HTML/XML - 如何評估字符串上的xpath并返回結(jié)果字符串

我們想知道如何評估字符串上的xpath并返回結(jié)果字符串。
import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;

public class Main {

    public static void main(String[] args) throws Exception {
        String xml = "<car><manufacturer>toyota</manufacturer></car>";
        String xpath = "/car/manufacturer";
        XPath xPath = XPathFactory.newInstance().newXPath();
        assertEquals("toyota",xPath.evaluate(xpath, new InputSource(new StringReader(xml))));
    }

}