将复杂的XML转换为CSV

时间:2020-07-14 14:55:32

标签: python xml parsing

我有这样结构的XML:

<result>
    <report>
        <id>111</id>
        <user>username1</user>
        <actions_list>
            <action1>
                <id>a_1</id>
            </action1>
            <action1>
                <id>a_2</id>
            </action1>
            <action1>
                <id>a_3</id>
            </action1>
        </actions_list>
    </report>

    <report>
        <id>222</id>
        <user>username2</user>
        <actions_list>
            <action1>
                <id>a_1</id>
            </action1>
            <action2>
                <id>a_2</id>
            </action2>
            <action3>
                <id>a_3</id>
            </action3>
            <action4>
                <id>a_4</id>
            </action4>
            <action5>
                <id>a_5</id>
            </action5>
        <actions_list>
    </report>
</result>

因此,我想创建一个具有以下结构的CSV文件:

+---+-----+-----------+-----+
| 1 | 111 | username1 | a_1 |
+---+-----+-----------+-----+
| 1 | 111 | username1 | a_2 |
+---+-----+-----------+-----+
| 1 | 111 | username1 | a_3 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_1 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_2 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_3 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_4 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_5 |
+---+-----+-----------+-----+

我尝试使用python BeautifulSoup和xml.etree,但是无法处理具有相同名称(在我的示例中为'id')且在不同报告中操作次数不同的字段。我应该怎么做?任何帮助将非常感激。预先感谢。

1 个答案:

答案 0 :(得分:-1)

尝试以下操作:

public static void main(String[] args) {
    PointerByReference pointer = new PointerByReference();
    DLLLibrary.INSTANCE.test(pointer);

    assert pointer.getValue().getInt(0) == 123321; //this works
    A a = new A(pointer.getValue());
    //assert a.next.num == 456;  //excepted action
    a.read(); //java.lang.Error: Invalid memory access
}