How can I read in XML data that looks like this into Pandas? The code executes, but contains no information:
import pandas as pd
xml1 = '''<?xml version="1.0" encoding="utf-8"?>
<Inventory SchemaVersion="1.0" ItemID="TestItem">
<SpecificDeviceGroup>
<Device Count="2" Description="Base System Device">
<HardwareIDs>
<ID Value="testvaluehere" />
</HardwareIDs>
</Device>
<Device Count="1" Description="PCI Express" Class="System">
<HardwareIDs>
<ID Value="testvaluehere2" />
</HardwareIDs>
</Device>
</SpecificDeviceGroup>
</Inventory>
'''
pd.read_xml(xml1) # no data
CodePudding user response:
The function read_xml
takes the first nested version as the dataframe data. See also the documentation.
In your case that is the within the <Inventory>
tag, but that contains no list of data. You can specify where to start using xpath
within read_xml
.
The following code gets me a short DataFrame, with columns count, Description, HardWareIDs, Class.
import pandas as pd
xml1 = '''<?xml version="1.0" encoding="utf-8"?>
<Inventory SchemaVersion="1.0" ItemID="TestItem">
<SpecificDeviceGroup>
<Device Count="2" Description="Base System Device">
<HardwareIDs>
<ID Value="testvaluehere" />
</HardwareIDs>
</Device>
<Device Count="1" Description="PCI Express" Class="System">
<HardwareIDs>
<ID Value="testvaluehere2" />
</HardwareIDs>
</Device>
</SpecificDeviceGroup>
</Inventory>
'''
pd.read_xml(xml1, xpath = ".//Device")