I have an Xml w/c look like this: - <xml>
-
<process name="proc1">
-
<mkdir>directory</mkdir>
-
<copyfile>src,dst</copyfile>
-
</process>
-
-
<process name="proc2">
-
<copyfile>src,dst</copyfile>
-
</process>
-
</xml>
then my problem is how can I get the child nodes of process w/c are "proc1" and "proc2"?
then i also need to get the value of these child nodes(example "src,dst" for <copyfile>)..
im using xml.dom.minidom module
Im hoping for your response guys...
8 1486
anyone knows???pls help...
bvdet 2,851
Expert Mod 2GB
You will need to create a parser, something like this: - from xml.dom.minidom import parse
-
-
fn = 'sample.xml'
-
-
dom1 = parse(fn)
-
# global variable required by handleData
-
nameList = ["proc1", "proc2"]
-
-
def getText(nodelist):
-
rc = ""
-
for node in nodelist:
-
if node.nodeType == node.TEXT_NODE:
-
rc = rc + node.data
-
return rc
-
-
def handleData(nodelist, *args):
-
resList = []
-
for node in nodelist:
-
if str(node.attributes["name"].value) in nameList:
-
for arg in args:
-
resList.append(node.getElementsByTagName(arg))
-
return [item[0] for item in resList if item]
-
-
for item in dom1.getElementsByTagName("copyfile"):
-
print getText(item.childNodes)
-
-
process_elements = dom1.getElementsByTagName('process')
-
print process_elements
-
-
print handleData(process_elements, "mkdir", "copyfile")
-
-
for item in handleData(process_elements, "mkdir", "copyfile"):
-
print getText(item.childNodes)
Contents of sample.xml: <xml>
<process name="proc1">
<mkdir>directory</mkdir>
<copyfile>src,dst</copyfile>
</process>
<process name="proc2">
<copyfile>src,dst</copyfile>
</process>
<process name="proc3">
<mkdir>directory</mkdir>
<copyfile>src,dst</copyfile>
</process>
<process name="proc4">
<mkdir>directory</mkdir>
<copyfile>src,dst</copyfile>
</process>
</xml> Output from above code: >>> src,dst
src,dst
src,dst
src,dst
[<DOM Element: process at 0xed2670>, <DOM Element: process at 0xed2f58>, <DOM Element: process at 0xedb4b8>, <DOM Element: process at 0xedb788>]
[<DOM Element: mkdir at 0xed2e68>, <DOM Element: copyfile at 0xed2e90>, <DOM Element: copyfile at 0xedb0a8>]
directory
src,dst
src,dst
>>>
You will need to create a parser, something like this: - from xml.dom.minidom import parse
-
-
fn = 'sample.xml'
-
-
dom1 = parse(fn)
-
# global variable required by handleData
-
nameList = ["proc1", "proc2"]
-
-
def getText(nodelist):
-
rc = ""
-
for node in nodelist:
-
if node.nodeType == node.TEXT_NODE:
-
rc = rc + node.data
-
return rc
-
-
def handleData(nodelist, *args):
-
resList = []
-
for node in nodelist:
-
if str(node.attributes["name"].value) in nameList:
-
for arg in args:
-
resList.append(node.getElementsByTagName(arg))
-
return [item[0] for item in resList if item]
-
-
for item in dom1.getElementsByTagName("copyfile"):
-
print getText(item.childNodes)
-
-
process_elements = dom1.getElementsByTagName('process')
-
print process_elements
-
-
print handleData(process_elements, "mkdir", "copyfile")
-
-
for item in handleData(process_elements, "mkdir", "copyfile"):
-
print getText(item.childNodes)
Contents of sample.xml:<xml>
<process name="proc1">
<mkdir>directory</mkdir>
<copyfile>src,dst</copyfile>
</process>
<process name="proc2">
<copyfile>src,dst</copyfile>
</process>
<process name="proc3">
<mkdir>directory</mkdir>
<copyfile>src,dst</copyfile>
</process>
<process name="proc4">
<mkdir>directory</mkdir>
<copyfile>src,dst</copyfile>
</process>
</xml> Output from above code:>>> src,dst
src,dst
src,dst
src,dst
[<DOM Element: process at 0xed2670>, <DOM Element: process at 0xed2f58>, <DOM Element: process at 0xedb4b8>, <DOM Element: process at 0xedb788>]
[<DOM Element: mkdir at 0xed2e68>, <DOM Element: copyfile at 0xed2e90>, <DOM Element: copyfile at 0xedb0a8>]
directory
src,dst
src,dst
>>>
thanks bvdet....i'll try this one..thanks
bvdet 2,851
Expert Mod 2GB
thanks bvdet....i'll try this one..thanks
You are welcome. I am learning about XML and DOM also.
You are welcome. I am learning about XML and DOM also.
I know this is too much :-).
I want to ask another favor..What if i want the output should look like this:
process name="proc1"
mkdir: directory
copyfile: src,dst
process name="proc2"
copyfile: src,dst
process name="proc3"
mkdir: directory
copyfile>src,dst
and how can i parse an xml childnode w/c look like this:
<download ='ftp' user='username' password='password'>
thanks in advance bvdet..Hope you can help me with this in a second time...
bvdet 2,851
Expert Mod 2GB
I know this is too much :-).
I want to ask another favor..What if i want the output should look like this:
process name="proc1"
mkdir: directory
copyfile: src,dst
process name="proc2"
copyfile: src,dst
process name="proc3"
mkdir: directory
copyfile>src,dst
and how can i parse an xml childnode w/c look like this:
<download ='ftp' user='username' password='password'>
thanks in advance bvdet..Hope you can help me with this in a second time...
Create a function to format the data: - from xml.dom.minidom import parse
-
-
# global variables required by formatData
-
nameList = ["proc1", "proc2"]
-
nodeIDlist = ['name',]
-
-
def formatData(nodelist, *args):
-
resList = []
-
for node in nodelist:
-
for id in nodeIDlist:
-
try:
-
s = str(node.attributes[id].value)
-
if s in nameList:
-
resList.append('%s name=%s' % (repr(elem.parentNode).split(':')[1].split()[0], s))
-
for arg in args:
-
try:
-
resList.append(' %s: %s' % (arg, getText(node.getElementsByTagName(arg)[0].childNodes)))
-
except IndexError, e:
-
# print 'Invalid element tag: %s' % arg
-
pass
-
except KeyError, e:
-
# print 'Invalid node atribute:', e
-
pass
-
return '\n'.join(resList)
-
-
dom1 = parse('sample.xml')
- >>> process_elements = dom1.getElementsByTagName('process')
-
>>> process_elements
-
[<DOM Element: process at 0xf8bb98>, <DOM Element: process at 0xf8b918>, <DOM Element: process at 0xf8b710>, <DOM Element: process at 0xf87490>]
-
>>> print formatData(process_elements, "mkdir", "copyfile")
-
process name=proc1
-
mkdir: directory1
-
copyfile: src1,dst1
-
process name=proc2
-
copyfile: src2,dst2
-
>>>
The string <download ='ftp' user='username' password='password'> does not appear to be valid XML. Should not there be an attribute name to the left of the equal sign after 'download'?
Create a function to format the data: - from xml.dom.minidom import parse
-
-
# global variables required by formatData
-
nameList = ["proc1", "proc2"]
-
nodeIDlist = ['name',]
-
-
def formatData(nodelist, *args):
-
resList = []
-
for node in nodelist:
-
for id in nodeIDlist:
-
try:
-
s = str(node.attributes[id].value)
-
if s in nameList:
-
resList.append('%s name=%s' % (repr(elem.parentNode).split(':')[1].split()[0], s))
-
for arg in args:
-
try:
-
resList.append(' %s: %s' % (arg, getText(node.getElementsByTagName(arg)[0].childNodes)))
-
except IndexError, e:
-
# print 'Invalid element tag: %s' % arg
-
pass
-
except KeyError, e:
-
# print 'Invalid node atribute:', e
-
pass
-
return '\n'.join(resList)
-
-
dom1 = parse('sample.xml')
- >>> process_elements = dom1.getElementsByTagName('process')
-
>>> process_elements
-
[<DOM Element: process at 0xf8bb98>, <DOM Element: process at 0xf8b918>, <DOM Element: process at 0xf8b710>, <DOM Element: process at 0xf87490>]
-
>>> print formatData(process_elements, "mkdir", "copyfile")
-
process name=proc1
-
mkdir: directory1
-
copyfile: src1,dst1
-
process name=proc2
-
copyfile: src2,dst2
-
>>>
The string <download ='ftp' user='username' password='password'> does not appear to be valid XML. Should not there be an attribute name to the left of the equal sign after 'download'?
it actually look like this..
<process name='download'>
<download server='ftp' user='username' password='******'>
<destination>path</destination>
<unzip>*.jpg, *.doc, *.pdf</unzip>
</download>
</process>
Actually I'm making a program right now and its output depends on the xml.
You help a me a lot bvdet..Thanks man
bvdet 2,851
Expert Mod 2GB
I have played around with XML parsing, and I made a new function. It is kind of ugly and does not work exactly the way I want, so maybe someone can improve it. Following is the complete code: - from xml.dom.minidom import parse
-
-
def getText(nodelist):
-
rc = []
-
for node in nodelist:
-
if node.nodeType == node.TEXT_NODE:
-
s = node.data.strip()
-
if s:
-
rc.append(node.data)
-
return '\n'.join(rc)
-
-
def nodeName(node):
-
try: return repr(node).split(':')[1].split()[0]
-
except: return ''
-
-
def getDataList(nodelist, **kargs):
-
resList = []
-
for node in nodelist:
-
node_name = nodeName(node)
-
if node_name in kargs:
-
keys = kargs[node_name].keys()
-
for id in keys:
-
try:
-
s = str(node.attributes[id].value)
-
v = kargs[node_name][id]
-
if not v or s in kargs[node_name][id]:
-
resList.append('%s %s=%s' % (node_name, id, s))
-
-
if node.nodeType == node.ELEMENT_NODE:
-
nodes = node.childNodes
-
name = node.nodeName
-
print 'DOM element = %s' % name
-
s = []
-
for elem in nodes:
-
nm = nodeName(elem)
-
s.append(' %s%s' % (['', nm+': '][len(nm)>0 or 0],getText(elem.childNodes)))
-
print '\n'.join([i for i in s if i.strip()])
-
elif node.nodeType == node.TEXT_NODE:
-
s = getText(node)
-
print 'Text Node Text = %s' % s
-
-
except KeyError, e:
-
print 'Invalid node atribute:', e
-
pass
-
return resList
-
-
fn = r'H:\TEMP\temsys\sampleXML.txt'
-
-
dom1 = parse(fn)
-
-
process_elements = dom1.getElementsByTagName('process')
-
download_elements = dom1.getElementsByTagName('download')
-
-
elemDict = {'process': {'name': ["proc1", "proc2"]}, 'download': {'server': ['ftp', ]}}
-
x = getDataList(process_elements, **elemDict)
-
y = getDataList(download_elements, **elemDict)
-
-
print
-
print x
-
print y
Output: >>> DOM element = process
mkdir: directory1
mkdir: directory11
mkdir: directory111
copyfile: src1,dst1
DOM element = process
copyfile: src2,dst2
DOM element = download
destination: path
unzip: *.jpg, *.doc, *.pdf
['process name=proc1', 'process name=proc2']
['download server=ftp']
>>> Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by Dariusz |
last post: by
|
reply
views
Thread by Andy |
last post: by
|
1 post
views
Thread by Magnus |
last post: by
|
6 posts
views
Thread by KevinD |
last post: by
|
6 posts
views
Thread by arne.muller |
last post: by
|
10 posts
views
Thread by Tyler |
last post: by
| |
4 posts
views
Thread by Shark |
last post: by
|
13 posts
views
Thread by swetha |
last post: by
|
6 posts
views
Thread by efrenba |
last post: by
| | | | | | | | | | |