Dipesh Deb Dipesh Deb - 8 months ago 48
Python Question

Python getting xml same childnodes in same line

I have a xml file as:

<?xml version="1.0" encoding="utf-8"?>

where node depends on user input. In "Dir" node it may be 1,2,5,10 dir structure defined.
I am able to extract data from the xml using below Python code:

from xml.dom import minidom

dom = minidom.parse('Test0001.xml')

for node in Src:
alist =node.getElementsByTagName('Dir')
for a in alist:
dirtext = a.childNodes[0].nodeValue + ','
print dirtext

I am getting Output in multi line as:


But I need the output in single line without space and remove last comma, like:


Please help me in this regard, I have tried a lot... It may be by using itertools grouping or defaultdict. Any help is greatly appreciated.


I presume you are trying to store the string so use str.join:

output = ",".join([a.childNodes[0].nodeValue for node in Src for a in node.getElementsByTagName('Dir')])

You could add a trailing comma after the print so you don't get a newline printed after eachprint dirte, but that won't account for the trailing comma and does not help you if you actually want to store the string.


In [1]: from xml.dom import minidom

In [2]: x = r"""<?xml version="1.0" encoding="utf-8"?>
   ...:   <SetupConf>
   ...:   <LocSetup>
   ...:     <Src>
   ...:       <Dir>C:\User1\test1</Dir>
   ...:       <Dir>C:\User2\log</Dir>
   ...:       <Dir>D:\Users\Checkup</Dir>
   ...:       <Dir>D:\Work1</Dir>
   ...:       <Dir>E:\job1</Dir>
   ...:      </Src>
   ...:   </LocSetup>
   ...:   </SetupConf>"""

In [3]: from StringIO import StringIO

In [4]: 

In [4]: dom = minidom.parse(StringIO(x))

In [5]: Src = dom.getElementsByTagName('Src')

In [6]: output = ",".join([a.childNodes[0].nodeValue for node in Src for a in node.getElementsByTagName('Dir')])

In [7]: print(output)