[英]Facing issues while parsing XML with namespaces in python while using lxml
I am trying to access and modify a tag deep with in the hierarchy of an XML . 我正在尝试访问和修改XML层次结构中的标记。 I have used quite a few options to reach it .
我已经使用了很多选择来实现它。 Please help me accessing and modifying the tag .
请帮助我访问和修改标签。 Here is my XML :
这是我的XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cre="http://www.code.com/abc/V1/createCase">
<soapenv:Header><wsse:Security xmlns:wsse="http://docs.oasis-open.org/2" xmlns:wsu="http://docs.oasis-open.org/a.xsd"></wsse:Security>
</soapenv:Header>
<soapenv:Body xmlns:wsu="http://docs.oasis-open.org/30.xsd" wsu:Id="id-14">
<cre:createCase>
<cre:Request>
<cre:ServiceAttributesGrp>
<cre:MinorVer>?</cre:MinorVer>
</cre:ServiceAttributesGrp>
<cre:CreateCaseReqGrp>
<cre:Language>English</cre:Language>
<cre:CustFirstNm>Issue</cre:CustFirstNm>
<cre:CustLastNm>Detection</cre:CustLastNm>
<cre:AddlDynInfoGrp>
<cre:AddlDynInfo>
<cre:FieldNm>TM3</cre:FieldNm>
<cre:FieldVal></cre:FieldVal>
</cre:AddlDynInfo>
<cre:AddlDynInfo>
<cre:FieldNm>PM417</cre:FieldNm>
<cre:FieldVal>Not Defined</cre:FieldVal>
</cre:AddlDynInfo>
</cre:AddlDynInfoGrp>
<cre:CreateCriteriasGrp>
<cre:CreateCriterias>
<cre:CriteriaNm>CriticalReqDtlValidationReqd</cre:CriteriaNm>
</cre:CreateCriterias>
</cre:CreateCriteriasGrp>
</cre:CreateCaseReqGrp>
</cre:Request>
</cre:createCase>
</soapenv:Body>
</soapenv:Envelope>
I have to access and modify the value of "FieldVal" tag in "AddlDynInfo" Tag , where the corresponding value of "FieldNm" tag value is "PM417" (since there are two occurances of "AddlDynInfo" tag . As of now , I am stuck on the parent tag only , as I could not access it : 我必须访问和修改“ AddlDynInfo”标签中“ FieldVal”标签的值,其中“ FieldNm”标签值的对应值为“ PM417”(因为“ AddlDynInfo”标签有两次出现。)只停留在父标记上,因为我无法访问它:
tree = etree.parse(template_xml)
root = tree.getroot()
for msgBody in root[1]:
for createCase in msgBody:
for request in createCase:
print request
for CreateCaseReqGrp in request.findall('{cre}CreateCaseReqGrp',namespaces=root.nsmap):
print CreateCaseReqGrp
Defined namespaces and XPaths make this quite easy. 定义的名称空间和XPath使这变得非常容易。 Your case would be something like this:
您的情况将是这样的:
ns = {
'soapenv': 'http://schemas.xmlsoap.org/soap/envelope/',
'cre': 'http://www.code.com/abc/V1/createCase'
}
for casereq in root.xpath(
'soapenv:Body/cre:createCase/cre:Request/'
'cre:CreateCaseReqGrp/cre:AddlDynInfoGrp/cre:AddlDynInfo', namespaces=ns):
print casereq.xpath('cre:FieldNm/text()', namespaces=ns)
print casereq.xpath('cre:FieldVal/text()', namespaces=ns)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.