Can';t在python中使用名称空间解析xml
我试图用Can';t在python中使用名称空间解析xml,python,xml,xpath,lxml,elementtree,Python,Xml,Xpath,Lxml,Elementtree,我试图用lxml和elementree在python中解析xml,但由于名称空间的原因,它无法工作 我尝试了xpath,但没有成功。 还有,如何将xml文档转换为utf-8格式,因为现在我需要将其添加到xml中进行解析 data = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <![CDATA[<?xml version='1.0' encoding='UT
lxml
和elementree
在python中解析xml,但由于名称空间的原因,它无法工作
我尝试了xpath,但没有成功。
还有,如何将xml文档转换为utf-8格式,因为现在我需要将其添加到xml中进行解析
data = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<![CDATA[<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Header>
<messageHeader:messageHeader xmlns:messageHeader="http://www.xyx.co.nz/ismm/common/messageHeader/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="messageHeader:MessageHeader">
<messageHeader:application>THOM</messageHeader:application>
<messageHeader:transactionId>BVCQWAC</messageHeader:transactionId>
<messageHeader:correlationId>1771518</messageHeader:correlationId>
<messageHeader:timeStamp>2016-11-18T20:41:16</messageHeader:timeStamp>
</messageHeader:messageHeader>
</soapenv:Header>
<soapenv:Body>
<submitSupplierPartner xmlns:customerBill="http://www.xyzaaa.com/ismm/common/customerBill/v1" xsi:type="messaging_supplierPartner:SubmitSupplierPartner">
<logisticsOrder>
<interactionDateTime>2016-11-18T20:41:16</interactionDateTime>
<businessInteractionRole xsi:type="bi:PartyInteractionRole">
<interactionRole>Customer</interactionRole>
<partyRole xsi:type="customer:Customer">
<contactMedium xsi:type="party:DeliveryContact">
.....
.....
namespace = "http://schemas.xmlsoap.org/soap/envelope/"
namespace_c = "{" + namespace + "}"
NSMAP = {"soapenv": namespace}
root = lxml.etree.fromstring(data)
# for i, element in enumerate(root.getiterator()):
# print(element.tag)
#get data from header tag
records = root.xpath('//messageHeader:messageHeader/messageHeader:correlationId', namespaces = {'messageHeader': 'http://www.xyx.co.nz/ismm/common/messageHeader/v1'})
for record in records:
print(record.text)
#get data from body tag
records = root.xpath('//submitSupplierPartner', namespaces = {"customerBill": "http://www.xyzaaa.com/ismm/common/customerBill/v1"})
for record in records:
print(record.text)
data=”“”
汤姆
BVCQWAC
1771518
2016-11-18T20:41:16
2016-11-18T20:41:16
顾客
.....
.....
名称空间=”http://schemas.xmlsoap.org/soap/envelope/"
名称空间_c=“{”+名称空间+“}”
NSMAP={“soapenv”:名称空间}
root=lxml.etree.fromstring(数据)
#对于i,枚举中的元素(root.getiterator()):
#打印(element.tag)
#从标题标记获取数据
records=root.xpath('//messageHeader:messageHeader/messageHeader:correlationId',名称空间={'messageHeader':'http://www.xyx.co.nz/ismm/common/messageHeader/v1'})
记录中的记录:
打印(记录.文本)
#从body标签获取数据
records=root.xpath('//submitSupplierPartner',名称空间={“customerBill”:http://www.xyzaaa.com/ismm/common/customerBill/v1"})
记录中的记录:
打印(记录.文本)
这对我很有效
xml中的数据id
这对我有用
xml中的数据id
你读过/试过或吗?你读过/试过或吗?
root = lxml.etree.fromstring(data)
#orderId / uniqueCreatorId
records = root.xpath('//submitSupplierPartner/logisticsOrder/orderId')
for record in records:
orderID=record.text
print(orderID)
#sim and devices ID
hardwareID = []
records = root.xpath('//submitSupplierPartner/logisticsOrder/resourceOrderItem/resourceSpecification/ID')
for record in records:
hardwareID.append(record.text)
print(hardwareID)
#get the no of items for shipping
noOfItems = len(hardwareID)
print("Total items for shipping are :")
print(noOfItems)
#sim and devices skuNumber
hardwaresku = []
records = root.xpath('//submitSupplierPartner/logisticsOrder/resourceOrderItem/resourceSpecification/skuNumber')
for record in records:
hardwaresku.append(record.text)
print(hardwaresku)
#sim and devices itemId
hardwareitemID = []
records = root.xpath('//submitSupplierPartner/logisticsOrder/resourceOrderItem/itemId')
for record in records:
hardwareitemID.append(record.text)
print(hardwareitemID)
#correlation ID / Rom
records = root.xpath('//messageHeader:messageHeader/messageHeader:correlationId/text()',namespaces={"messageHeader": "http://www.xyz.co.nz/ismm/common/messageHeader/v1"})
for record in records:
correlationID = record
print(record)