Unable to evaluate expression using this context

Unable to evaluate expression using this context

JAVA语言使用Xpath解析XML格式字符串

public String xmlText(String xml) {

		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xml);//转为xml
			XPathFactory factory = XPathFactory.newInstance();
		    XPath xpath = factory.newXPath();
		    
			Node node =  (Node) xpath.evaluate("/*", doc, XPathConstants.NODE);
			System.out.println(node.getName() + "--------"
					+ node.getStringValue());

		} catch (XPathExpressionException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return node.getName();
	}

这是导入的jar包

import java.util.Iterator;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

报错信息

javax.xml.transform.TransformerException: Unable to evaluate expression using this context
	at com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source)
	at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source)
	at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source)
	at cn.com.test.dom4j.xmlText(dom4j.java:2971)
	at cn.com.test.dom4j.main(dom4j.java:2960)
Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context
	at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(Unknown Source)
	at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(Unknown Source)
	... 5 more
---------


请问大神如何解救我。。。。。哇呜呜。。。。。。。。

Unable to evaluate expression using this context_第1张图片

你可能感兴趣的:(java,Xpath,XML)