Thread: python xml

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0

    python xml


    Hi folks,

    how are we all, good i hope

    well im really struggling to understand how all the elements, childs and node go together and work.

    Code:
    xml structure
    
    <channels>
    	<channel>
    		<search>OLD</search>
    		<replace>NEW</replace>
    	</channel>
    	<channel>
    		<search>OLD 2</search>
    		<replace>NEW 2</replace>
    	</channel>
    </channels>
    
    python code
    
    from xml.dom import minidom
    
    doc = minidom.parse("config.xml")
    
    map = {}
    
    for node in doc.getElementsByTagName('channel'):
    	s = node.getElementsByTagName('search')
    	r = node.getElementsByTagName('replace')
    	for sd, rd in zip(s, r):
    		map[sd.firstChild.data] = rd.firstChild.data
    
    print map
    now even tho this seems to work fine i have a feeling its not the correct way to go about it?

    seems like you have to know what every childs name is in order to use it, which means every change to the xml would require a code change as well?

    say for example i had 10 childs under <channel> so does that mean i would have to do

    c1 = node.getElementsByTagName('child1')
    ...
    c10 = node.getElementsByTagName('child10')

    seems silly to me, why cant just access the element <channel> and loop through everything under that to get whats there without knowing?

    or what about accessing by names like channel.search.data

    ive read the manual, other sites just doesnt seem explained to me or im a complete moron 1 of 2

    could maybe someone explain this better with some examples i would really appreciate that.

    many thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    10

    you might want to try this


    Code:
    root=doc.firstChild
    children = root.node.childNodes 
    for child in children:
      #do something with child
    you can write a recursive function to traverse the whole structure .
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0
    hi there,

    thanks for the reply but still confused

    root=doc.firstChild
    this is going to get the first child which will be channels

    children = root.node.childNodes

    stuck here as "node" is not an attribute of an element.

    do u have a working example with some explanation as that will really help

    thanks
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    10

    oops that was typo


    Oops looks like there was a typo there


    Code:
    root=doc.firstChild # this would give you <channels> node 
    children = root.childNodes #thiswould give you the two childre
    for child in children:
      #do something with child 
      #you might want to do something like 
      # child.childNodes to traverse further
      # or write a recursive function

IMN logo majestic logo threadwatch logo seochat tools logo