在Java中验证xpath表达式 [英] validate an xpath expression in Java

查看:337
本文介绍了在Java中验证xpath表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定用户输入字符串,如何判断它是否是Java中的
有效Xpath表达式。只是好奇,因为我不能
找到一种方法来使用javax.xml.xpath库。谢谢。

Given an user input string, how can I tell whether it is a valid Xpath expression or not in Java. Just curious, since I cannot find a way to do it using javax.xml.xpath library. Thanks.

推荐答案

我假设您要验证语法,但如果表达式在您的xml或xml架构。您可以使用编译(..)方法,如果xpath与语法不正确,它将抛出异常。

I assume you want to validate the syntax but not if the expression is valid within the context of a your xml or xml schema. You can use the compile(..) method and it will throw an exception if the xpath is incorrect w.r.t the syntax.

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//book[author='Abc']/title/text()");

这篇关于在Java中验证xpath表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆