Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 27th, 2004, 02:43 PM
rockets12345 rockets12345 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 101 rockets12345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 50 m 20 sec
Reputation Power: 10
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

Reply With Quote
  #2  
Old May 27th, 2004, 03:38 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Element instances've got previousSibling and nextSibling attributes.

Reply With Quote
  #3  
Old May 27th, 2004, 05:32 PM
rockets12345 rockets12345 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 101 rockets12345 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 50 m 20 sec
Reputation Power: 10
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

Reply With Quote
  #4  
Old May 27th, 2004, 06:04 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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.

Reply With Quote
  #5  
Old May 27th, 2004, 06:28 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,585 DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level)DevCoach User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Parsing with 'DOM' way to get current Node's previous and next ELEMENT_NODE names:

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap