如果我有多个标签,如何解析值,如上面的代码。
<formula1>
<team name="McLaren">
<driver>
<name>Abcd</name>
<points>112</points>
</driver>
<driver>
<name>Juan Pablo Montoya</name>
<points>60</points>
</driver>
</team>
</formula1>
<formula1>
<team name="McLaren">
<driver>
<name>Kimi Raikkonen</name>
<points>112</points>
</driver>
<driver>
<name>EFGH</name>
<points>60</points>
</driver>
</team>
</formula1>
我可以使用以下代码示例在第一个formula1()中的标签之间打印值:
ezxml_t f1 = ezxml_parse_file("formula1.xml");
printf("%s\n", ezxml_get(f1, "team", 0, "driver", 1, "name", -1)->txt);
ezxml_free(f1);
但不知道如何打印第二个值。
答案 0 :(得分:1)
选项1.换行
的xml:
<root>
<formula1>
<team name="McLaren">
<driver>
<name>Abcd</name>
<points>112</points>
</driver>
<driver>
<name>Juan Pablo Montoya</name>
<points>60</points>
</driver>
</team>
</formula1>
<formula1>
<team name="McLaren">
<driver>
<name>Kimi Raikkonen</name>
<points>112</points>
</driver>
<driver>
<name>EFGH</name>
<points>60</points>
</driver>
</team>
</formula1>
</root>
代码:
printf("%s\n", ezxml_get(f1, "formula1", 0, "team", 0, "driver", 1, "name", -1)->txt);
printf("%s\n", ezxml_get(f1, "formula1", 1, "team", 0, "driver", 0, "name", -1)->txt);
选项2.统一
的xml:
<formula1>
<team name="McLaren">
<driver>
<name>Abcd</name>
<points>112</points>
</driver>
<driver>
<name>Juan Pablo Montoya</name>
<points>60</points>
</driver>
</team>
<team name="McLaren">
<driver>
<name>Kimi Raikkonen</name>
<points>112</points>
</driver>
<driver>
<name>EFGH</name>
<points>60</points>
</driver>
</team>
</formula1>
代码:
printf("%s\n", ezxml_get(f1, "team", 0, "driver", 1, "name", -1)->txt);
printf("%s\n", ezxml_get(f1, "team", 1, "driver", 0, "name", -1)->txt);