开发者

How to get some xml that comes before and a little from after a DOM Node

开发者 https://www.devze.com 2022-12-26 13:13 出处:网络
I am using java and I am pretty open to using w3c DOM or DOM4J at this point. So lets say I have a Node like a text node that I have found something interesting in, like say an occurrence of a substr

I am using java and I am pretty open to using w3c DOM or DOM4J at this point.

So lets say I have a Node like a text node that I have found something interesting in, like say an occurrence of a substring in the nodes text. If I want to get a string with a number characters preceding that node and a few characters after that node how may I do that? Basically I need to be able to display a snippet of the original xml around the occurrence of that string.

The problem I have with getting the parent node for example and then calling asXML is that I no longer know the exact location of the substring in the text node. If I search again for that string value in the parents xml then I may find 2 occurrences or many more if the parent has other children that contain an occurrence of that string.

Much appreciation if any one can answer this question.开发者_如何学Python


I haven't done anything with the DOM from Java in ages, so take this as pseudocode, not Java.

Basically, it boils down to something like this:

parent = node.getParentNode()
Node[] children = parent.getChildNodes()

for (Node child : children) {
    if (child == node) {
        // Do something different with the matched node
    } else {
        // do something with child
}
0

精彩评论

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