質問

I had issues in getting the text value of and nodes using lxml where the XML text has namespaces in it. I was using findall('Status') but the result was always coming to null.

I arrived at the following working code in the end....Is this the correct way of using lxml for fetching node values? Can i improve this further?

import lxml
xml_string='<?xml version="1.0" encoding="UTF-8"?> <SCPP:Response xmlns:SCPP="http://www.SCPP.com/XMLSchema"> <SCPP:RESP_BODY> <Seed>001335834994</Seed> </SCPP:RESP_BODY> <SCPP:RESP_HDR> <Status>00</Status> </SCPP:RESP_HDR> </SCPP:Response>'
root = etree.fromstring(xml_string)
nsmap = {}
for ns in root.xpath('//namespace::*'):
    if ns[0]:
            nsmap[ns[0]] = ns[1]

#Method 1
print 'Status is ' , root.xpath('//SCPP:RESP_HDR', namespaces=nsmap)[0].find('Status').text
print 'Seed is ' , root.xpath('//SCPP:RESP_BODY', namespaces=nsmap)[0].find('Seed').text

#Method 2
print 'Status is ' , root.findall('SCPP:RESP_HDR',namespaces=nsmap)[0].find('Status').text
print 'Seed is ' , root.findall('SCPP:RESP_BODY',namespaces=nsmap)[0].find('Seed').text

#Method 3   
print 'Status is ' , root.xpath('//SCPP:RESP_HDR', namespaces=nsmap)[0].find('Status').text
print 'Seed is ' , root.find('SCPP:RESP_BODY',namespaces=nsmap).find('Seed').text
役に立ちましたか?

解決

You don't need to build nsmap manually.

Replace following lines:

nsmap = {}
for ns in root.xpath('//namespace::*'):
    if ns[0]:
            nsmap[ns[0]] = ns[1]

with:

nsmap = root.nsmap

Another way to get text of specific element (using xpath):

>>> root.xpath('.//SCPP:RESP_HDR/Status/text()', namespaces=nsmap)[0]
'00'
>>> root.xpath('.//SCPP:RESP_BODY/Seed/text()',namespaces=nsmap)[0]
'001335834994'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top