java xpath 表达式,用于获取属性值的Xpath表达式在Java中失败

我试图从XML文件中获取属性值,但代码失败,出现以下异常:

11-15 16:34:42.270:DEBUG/XpathUtil(403):exception=javax.xml.xpath.XPathExpressionException:javax.xml.transform.TransformerException:额外的非法标记:“@”,“source”

下面是我用来获取节点列表的代码:

private static final String XPATH_SOURCE = "array/extConsumer@source";

mDocument = XpathUtils.createXpathDocument(xml);

NodeList fullNameNodeList = XpathUtils.getNodeList(mDocument,

XPATH_FULLNAME);

这是我的

XpathUtils

班级:

public class XpathUtils {

private static XPath xpath = XPathFactory.newInstance().newXPath();

private static String TAG = "XpathUtil";

public static Document createXpathDocument(String xml) {

try {

Log.d(TAG , "about to create document builder factory");

DocumentBuilderFactory docFactory = DocumentBuilderFactory

.newInstance();

Log.d(TAG , "about to create document builder ");

DocumentBuilder builder = docFactory.newDocumentBuilder();

Log.d(TAG , "about to create document with parsing the xml string which is: ");

Log.d(TAG ,xml );

Document document = builder.parse(new InputSource(

new StringReader(xml)));

Log.d(TAG , "If i see this message then everythings fine ");

return document;

} catch (Exception e) {

e.printStackTrace();

Log.d(TAG , "EXCEPTION OCCURED HERE " + e.toString());

return null;

}

}

public static NodeList getNodeList(Document doc, String expr) {

try {

Log.d(TAG , "inside getNodeList");

XPathExpression pathExpr = xpath.compile(expr);

return (NodeList) pathExpr.evaluate(doc, XPathConstants.NODESET);

} catch (Exception e) {

e.printStackTrace();

Log.d(TAG, "exception = " + e.toString());

}

return null;

}

// extracts the String value for the given expression

public static String getNodeValue(Node n, String expr) {

try {

Log.d(TAG , "inside getNodeValue");

XPathExpression pathExpr = xpath.compile(expr);

return (String) pathExpr.evaluate(n, XPathConstants.STRING);

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

我在

getNodeList

方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值