开发者

How Can I search for xml tag names in java

I need to search a specific xml node. I know part of its name but not the entire name... How can I get that element with part of the name?

I know it can be resolved using xpath, but I dont know how. For example...:

<root>
<head/>
<body> <SuperAsdf> hi </SuperAsdf>
</body>
<root/>

I know that have to be a tag with "Super" but I dont know that it could have "Asdf", so I need a xPath expression to find all the tags with "Super".

Als开发者_如何学Goo, what function supports xpaths in java?

I'm using getElementsByTagName, but I read that it cant interpretate xpath.


Regular expressions would be overkill for this purpose. Just use an XPath expression like:

"//*[starts-with(local-name(), 'Super')]"

Regarding Java functions for XPath... see the code here for example, or look at the class XPathFactory.


XPath 1.0 doesn't support regular expressions but you can still do something like this to achieve what you're trying to do (Using only XPath functions).

Xpath 2.0 support regular expressions with functions like matches() and replace() (more information here).

A regular expression in your case would look like:

(Super).*


You can use the XPath expression that LarsH suggested with the XPathAPI library (javadoc):

List<Node> nodes =
    XPathAPI.selectListOfNodes(doc, "//*[starts-with(local-name(), 'Super')]")

If you have an XPath-2.0-compliant processor (for example Saxon) on your class path, JAXP and XPathAPI will pick it up and use it to select nodes.

(Disclaimer, I'm the author of the XPathAPI library.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜