|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Parsing with 'DOM' way to get current Node's previous and next ELEMENT_NODE names:
Hi,
I am here again with another problem. When parsing an xml using 'DOM' is there a way to find out the current Node or Element's previous and next Node or Element. i.e. <nd> <test1 cd="1"> some text 1 </test1> <test2 cd="2"> some text 2 </test2> <test2 cd="2"> some text different than the above </test2> <test3 cd="3"> some text 3 </test3> </nd> Now let's say if I am at (during parse) <test2 cd="2">some text 2</test2> and I want to get the name of the next node i.e <test2 cd="2">some text different than the above</test2> just to make sure that this node is there for the proper sequence. And also when I am at (during parse) <test2 cd="2">some text different than the above</test2> and I want to get the name of the previous node i.e <test2 cd="2">some text 2</test2> just to make sure that this node is there for the proper sequence. Is there a way as using nextSibling and previousSibling doesn't work as it gives the 'None' because of a new-line(text-node) believe so. Any help is appreciated. Thanks |
|
#2
|
|||
|
|||
|
Element instances've got previousSibling and nextSibling attributes.
|
|
#3
|
|||
|
|||
|
nextSibling and previousSibling doesn't work as it gives the 'None' because of a new-line(text-node) believe so. Mentioned in the post last-line
|
|
#4
|
|||
|
|||
|
Okay ... ? (Yeah. Sorry ... didn't read the whole message.) Which DOM are you using?
I parsed your xml snippet with xml.dom.minidom (PyXML 0.84). Every child of <nd> had proper a nextSibling and a proper previousSibling. The first child (<DOM Text node "\n ">) didn't have a previousSibling (as expected). But it did have a nextSibling (<DOM Element: test2 at ...>). (This also works fine without PyXML.) As for previousSibling and nextSibling giving back the text nodes instead of the nodes you want: Code:
def getprevrealnode(node):
if node.previousSibling is None:
return None
if node.previousSibling.nodeType == node.TEXT_NODE:
return getprevrealnode(node.previousSibling)
return node.previousSibling
def getnextrealnode(node):
if node.nextSibling is None:
return None
if node.nextSibling.nodeType == node.TEXT_NODE:
return getnextrealnode(node.nextSibling)
return node.nextSibling
While this is not good code, it works for your purpose (if you can solve the problem of text nodes giving you None for previous and next siblings). Last edited by percivall : May 27th, 2004 at 06:26 PM. |
|
#5
|
|||
|
|||
|
Perhaps you are using the wrong Node. In your example, <test2 cd="2">some text 2</test2> is actually composed of several Node objects:
* an Element node with the tagName of "test2", containing the following nodes: * an attributes nodeList, containing an Attr object with a name of "cd" and a value of "2" * childNodes - a list of Node objects, containing a Text object with a nodeValue of "some text 2" If you are calling previousSibling on the Attr or Text objects then they will return None, since they have no siblings. If you call it on the Element object then you should get the previous Node in the DOM. this will probably be a Text node containing the white-space between the Elements, so you will need to step back through the previousSiblings until you find an Element object or None. Dave - The Developers' Coach |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Parsing with 'DOM' way to get current Node's previous and next ELEMENT_NODE names: |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|