Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xml dict is converted into a disordered XML file, which can be unrecoverable #340

Open
yangsongbai1 opened this issue Dec 15, 2023 · 1 comment

Comments

@yangsongbai1
Copy link

When I have the following xml

<w:document xmlns:w="http://www.example.com/wordproc">
    <w:body>
        <w:p>
            abc
        </w:p>
        <w:b>
            def
        </w:b>
        <w:p>
            ghi
        </w:p>
        <w:i>
            gkl
        </w:i>
        <w:p>
            mno
        </w:p>
    </w:body>
</w:document>

It parses into json format like this

{
    "w:document":{
        "@xmlns:w":"http://www.example.com/wordproc",
        "w:body":{
            "w:p":[
                "abc",
                "ghi",
                "mno"
            ],
            "w:b":"def",
            "w:i":"gkl"
        }
    }
}

Restoring again will not guarantee the original order

<w:document
    xmlns:w="http://www.example.com/wordproc">
    <w:body>
        <w:p>abc</w:p>
        <w:p>ghi</w:p>
        <w:p>mno</w:p>
        <w:b>def</w:b>
        <w:i>gkl</w:i>
    </w:body>
</w:document>

I want to parse into the following format, how can I achieve this, I tried the force list argument and it didn't work

{
    "w:document": [
        {
            "@xmlns:w": "http://www.example.com/wordproc"
        },
        {
            "w:body": [
                {
                    "w:p": "abc",
                },
                {
                    "w:b": "def",
                },
                {
                    "w:p": "ghi",
                },
                {
                    "w:i": "gkl",
                },
                {
                    "w:p": "mno",
                }
            ]
        }
    ]
}
@pyhedgehog
Copy link

@yangsongbai1 You suggested result will become (after unparse()) something like:

<w:document
    xmlns:w="http://www.example.com/wordproc">
    <w:body><w:p>abc</w:p></w:body>
    <w:body><w:p>ghi</w:p></w:body>
    <w:body><w:p>mno</w:p></w:body>
    <w:body><w:b>def</w:b></w:body>
    <w:body><w:i>gkl</w:i></w:body>
</w:document>

There are related issue #247 that suggests better syntax (special $list key).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants