vector<pair<int, string>> attribute;
时间: 2023-12-17 14:05:44 浏览: 54
This is a declaration of a vector container named "attribute" that can store pairs of integer and string data types. Each element in the vector will be a pair consisting of an integer value and a corresponding string value.
相关问题
用python帮我把下面标签中的有效数据提取出来<annotation> <folder>converted/CMS/2D目标检测/filter</folder> <filename>converted/CMS/2D目标检测/filter_empty_target_img_after_hash2/0/20230401180910649_61.jpg</filename> <source> <database>Unknown</database> <annotation>Unknown</annotation> <image>Unknown</image> </source> <size> <width>1920</width> <height>1536</height> <depth></depth> </size> <segmented>0</segmented> <object> <name>二轮车</name> <truncated>0</truncated> <occluded>0</occluded> <difficult>0</difficult> <bndbox> <xmin>626.38</xmin> <ymin>808.12</ymin> <xmax>650.03</xmax> <ymax>852.04</ymax> </bndbox> <attributes> <attribute> <name>rotation</name> <value>0.0</value> </attribute> <attribute> <name>track_id</name> <value>6</value> </attribute> <attribute> <name>keyframe</name> <value>True</value> </attribute> </attributes> </object> <object> <name>行人</name> <truncated>0</truncated> <occluded>0</occluded> <difficult>0</difficult> <bndbox> <xmin>1612.74</xmin> <ymin>831.51</ymin> <xmax>1627.34</xmax> <ymax>873.8</ymax> </bndbox> <attributes> <attribute> <name>rotation</name> <value>0.0</value> </attribute> <attribute> <name>track_id</name> <value>8</value> </attribute> <attribute> <name>keyframe</name> <value>True</value> </attribute> </attributes> </object> <object> <name>行人</name> <truncated>0</truncated> <occluded>0</occluded> <difficult>0</difficult> <bndbox> <xmin>1469.0</xmin> <ymin>832.96</ymin> <xmax>1489.43</xmax> <ymax>865.8</ymax> </bndbox> <attributes> <attribute> <name>rotation</name> <value>0.0</value> </attribute> <attribute> <name>track_id</name> <value>9</value> </attribute> <attribute> <name>keyframe</name> <value>True</value> </attribute> </attributes> </object> </annotation>
你可以使用Python中的xml.etree.ElementTree库来解析XML数据并提取标签中的有效数据。以下是一个示例代码,演示如何提取出有效数据:
```python
import xml.etree.ElementTree as ET
# 定义XML字符串
xml_string = '''
<annotation>
<folder>converted/CMS/2D目标检测/filter</folder>
<filename>converted/CMS/2D目标检测/filter_empty_target_img_after_hash2/0/20230401180910649_61.jpg</filename>
<source>
<database>Unknown</database>
<annotation>Unknown</annotation>
<image>Unknown</image>
</source>
<size>
<width>1920</width>
<height>1536</height>
<depth></depth>
</size>
<segmented>0</segmented>
<object>
<name>二轮车</name>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>626.38</xmin>
<ymin>808.12</ymin>
<xmax>650.03</xmax>
<ymax>852.04</ymax>
</bndbox>
<attributes>
<attribute>
<name>rotation</name>
<value>0.0</value>
</attribute>
<attribute>
<name>track_id</name>
<value>6</value>
</attribute>
<attribute>
<name>keyframe</name>
<value>True</value>
</attribute>
</attributes>
</object>
<object>
<name>行人</name>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>1612.74</xmin>
<ymin>831.51</ymin>
<xmax>1627.34</xmax>
<ymax>873.8</ymax>
</bndbox>
<attributes>
<attribute>
<name>rotation</name>
<value>0.0</value>
</attribute>
<attribute>
<name>track_id</name>
<value>8</value>
</attribute>
<attribute>
<name>keyframe</name>
<value>True</value>
</attribute>
</attributes>
</object>
<object>
<name>行人</name>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>1469.0</xmin>
<ymin>832.96</ymin>
<xmax>1489.43</xmax>
<ymax>865.8</ymax>
</bndbox>
<attributes>
<attribute>
<name>rotation</name>
<value>0.0</value>
</attribute>
<attribute>
<name>track_id</name>
<value>9</value>
</attribute>
<attribute>
<name>keyframe</name>
<value>True</value>
</attribute>
</attributes>
</object>
</annotation>
'''
# 解析XML数据
root = ET.fromstring(xml_string)
# 提取有效数据
folder = root.find('folder').text
filename = root.find('filename').text
width = int(root.find('size/width').text)
height = int(root.find('size/height').text)
objects = []
for obj in root.findall('object'):
name = obj.find('name').text
xmin = float(obj.find('bndbox/xmin').text)
ymin = float(obj.find('bndbox/ymin').text)
xmax = float(obj.find('bndbox/xmax').text)
ymax = float(obj.find('bndbox/ymax').text)
objects.append({
'name': name,
'xmin': xmin,
'ymin': ymin,
'xmax': xmax,
'ymax': ymax
})
# 打印提取的有效数据
print("Folder:", folder)
print("Filename:", filename)
print("Width:", width)
print("Height:", height)
print("Objects:")
for obj in objects:
print(obj)
```
运行这段代码,它将解析XML字符串并提取出标签中的有效数据。你可以根据需要对提取出的数据进行进一步处理或存储。
使用dom4j 解析 Map<List<Map<String,Object>>> 结构的xml
好的,可以。首先,需要使用Java中的dom4j库来解析XML。然后,使用以下代码来解析Map<List<Map<String, Object>>>结构的XML:
``Document document = DocumentHelperText(xmlString);
Element root = document.getRootElement();
Map<String, List<Map<String, Object>>> map = new HashMap<>();
for (Iterator j = root.elementIterator(); j.hasNext();) {
Element el = (Element) j.next();
List<Map<String, Object>> list = new ArrayList<>();
for (Iterator i = el.elementIterator(); i.hasNext();) {
Element e = (Element) i.next();
Map<String, Object> attrMap = new HashMap<>();
for (Iterator k = e.attributeIterator(); k.hasNext();) {
Attribute attr = (Attribute) k.next();
attrMap.put(attr.getName(), attr.getValue());
}
List<Object> content = new ArrayList<>();
if (!e.isTextOnly()) {
for (Iterator l = e.elementIterator(); l.hasNext();) {
Element innerEl = (Element) l.next();
Map<String, Object> innerAttrMap = new HashMap<>();
for (Iterator m = innerEl.attributeIterator(); m.hasNext();) {
Attribute innerAttr = (Attribute) m.next();
innerAttrMap.put(innerAttr.getName(), innerAttr.getValue());
}
content.add(innerAttrMap);
}
} else {
content.add(e.getTextTrim());
}
attrMap.put("content", content);
list.add(attrMap);
}
map.put(el.getName(), list);
}
```
这段代码会将解析后的XML存储在一个Map<String, List<Map<String, Object>>>中,其中Map的key为节点名称,value为该节点下的所有子节点的List<Map<String, Object>>。
希望这个回答能够帮助到你。
阅读全文