tags:

views:

26

answers:

1

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.

A: 

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
}
Devon_C_Miller