[BangPypers] [Small Hitch]Minidom and xml parsing.

Nitin Kumar nitin.nitp at gmail.com
Tue Jul 12 10:44:19 CEST 2011


are you limited to use DOM?

if not then you can also use elementtree
for ex:

from xml.etree import ElementTree as ET
fp=open('try.xml')                         #your xml file
xmlData = fp.read()
tree = ET.XML( xmlData )
listroot = list(tree)

for item in listroot:
if(item.tag == "parent"):
for dcs in list(item):
dcs.tag


if you want to use dom parsing only then you can try this:

from xml.dom.minidom import parse
datasource = parse(fp)
for elem in datasource.getElementsByTagName('parent'):
for i in elem.childNodes:
i.nodeName
                print dir(i)

On Tue, Jul 12, 2011 at 12:20 PM, Subhodip Biswas
<subhodipbiswas at gmail.com>wrote:

> Hi all,
>
> I am fairly new to python and i am stuck with a problem.
> What I am  trying to do is parse a xml file using minidom. My xml file
> as almost a structure like this :
>
> <someparentabove>
>       <parent>
>              <child1>
>              </child1>
>              <child2>
>              </child2>
>      </parent>
>      <anotherparent>
>      </anotherparent>
> </someparentabove>
>
> I can get parent along with childs using getElementsByTagName, But is
> there a way I can get only the children (child1 and Child2)?
>
>
> -------------
> Thanks and Regards
> Subhodip Biswas
>
>
> GPG key : FAEA34AB
> Server : pgp.mit.edu
> http://subhodipbiswas.wordpress.com
> http:/www.fedoraproject.org/wiki/SubhodipBiswas
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
Nitin K


More information about the BangPypers mailing list