我已经进行了API调用,并希望遍历响应xml以将相关值提取到数据框。该代码之前运行良好,但现在显然不希望返回超出每个节点/列的第一个值。
这是我的回复XML:
<?xml version="1.0" encoding="utf-8"?>
<Assets xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<IsLastPage>true</IsLastPage>
<AssetRecords>
<Asset url="https://api.myvisionlink.com/APIService/VLReady/assets/single/1486128866430645">
<VisionLinkIdentifier>1486128866430645</VisionLinkIdentifier>
<MakeCode>CAT</MakeCode>
<MakeName>CAT</MakeName>
<SerialNumber>PNL00585</SerialNumber>
<AssetID>10-143</AssetID>
<EquipmentVIN/>
<Model>320ELRR</Model>
<ProductFamily>TRACK EXCAVATORS</ProductFamily>
<ManufactureYear>2015</ManufactureYear>
</Asset>
<Asset url="https://api.myvisionlink.com/APIService/VLReady/assets/single/2278960667345107">
<VisionLinkIdentifier>2278960667345107</VisionLinkIdentifier>
<MakeCode>CAT</MakeCode>
<MakeName>CAT</MakeName>
<SerialNumber>HBT20130</SerialNumber>
<AssetID>10-160</AssetID>
<EquipmentVIN/>
<Model>330FL</Model>
等
这是我的代码:
r = session.get("https://api.myvisionlink.com/APIService/VLReady/Assets/1", headers={'Content-Type':'application/xml'})
def getvalueofnode(node):
return node.text if node is not None else None
def main():
root = cET.fromstring(r.content)
ns = {"xsd":"http://fms-standard.com/rfms/v1.0.0/xsd/position",
"xsi":"http://fms-standard.com/rfms/v1.0.0/xsd/common/position"}
data_list = [{'Make': getvalueofnode(node.find('Asset/MakeName', ns)),
'SerialNumber': getvalueofnode(node.find('Asset/SerialNumber', ns)),
'AssetID': getvalueofnode(node.find('Asset/AssetID', ns)),
'Model': getvalueofnode(node.find('Asset/Model', ns)),
'ProductFamily': getvalueofnode(node.find('Asset/ProductFamily', ns)),
'ManufactureYear': getvalueofnode(node.find('Asset/ManufactureYear', ns))} for node in root]
global df_xml
df_xml = pd.DataFrame(data_list)
main()
答案 0 :(得分:1)
我不确定您从API调用中返回的结果,但是在您提供的问题示例中xml格式不正确。如果XML的结构不同,则您的代码可以正常工作,例如,资产元素位于XML结构的根目录中。
仅获得第一条记录的原因是因为您正在遍历“ IsLastPage”元素和“ AssetRecords”元素,并且由于您使用的是find()而不是findall(),因此它将停止一次找到第一个比赛。如果要继续使用find()而不是findall(),则必须修改代码以迭代“ AssetRecords”元素,这是我在下面的代码中修改的内容。
def main():
root = et.fromstring(xml)
ns = {"xsd":"http://fms-standard.com/rfms/v1.0.0/xsd/position",
"xsi":"http://fms-standard.com/rfms/v1.0.0/xsd/common/position"}
# Find AssetRecords element
asset_records = root.find("AssetRecords")
data_list = [{'Make': getvalueofnode(node.find('MakeName', ns)),
'SerialNumber': getvalueofnode(node.find('SerialNumber', ns)),
'AssetID': getvalueofnode(node.find('AssetID', ns)),
'Model': getvalueofnode(node.find('Model', ns)),
'ProductFamily': getvalueofnode(node.find('ProductFamily', ns)),
'ManufactureYear': getvalueofnode(node.find('ManufactureYear', ns))} for node in asset_records]
global df_xml
df_xml = pd.DataFrame(data_list)
Output:
Make SerialNumber AssetID Model ProductFamily ManufactureYear
0 CAT PNL00585 10-143 320ELRR TRACK EXCAVATORS 2015
1 CAT HBT20130 10-160 330FL TRACK EXCAVATORS 2015
希望能回答您的问题,如果您需要我进行澄清,请告诉我。 :)