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

dict to xml - how to discard the parent tag repeating for conversion from list #315

Open
sergei-sss opened this issue Oct 30, 2022 · 2 comments

Comments

@sergei-sss
Copy link

Hi everyone.

From this dictionary:

feed = {
    'feed': {
        'reviewer_images': [
            {
                'reviewer_image': {
                    'url': "http://google.com"
                }
            },
            {
                'reviewer_image': {
                    'url': "http://google.com"
                }
            }
        ]
    }
}

I have got:

<?xml version="1.0" encoding="utf-8"?>
<feed>
        <reviewer_images>
                <reviewer_image>
                        <url>http://google.com</url>
                </reviewer_image>
        </reviewer_images>
        <reviewer_images>
                <reviewer_image>
                        <url>http://github.com</url>
                </reviewer_image>
        </reviewer_images>
</feed>

Is it possible to get several reviewer_image inside identical reviewer_images tag?:

<?xml version="1.0" encoding="utf-8"?>
<feed>
        <reviewer_images>
                <reviewer_image>
                      <url>http://google.com</url>
                </reviewer_image>
                <reviewer_image>
                      <url>http://github.com</url>
                </reviewer_image>
        </reviewer_images>
</feed>
@thaikoh
Copy link

thaikoh commented Dec 8, 2022

Having the same issue, any help will be appreciated.

@bfontaine
Copy link

bfontaine commented Jul 7, 2023

A bit late to the party:

feed = {
    'feed': {
        'reviewer_images': {
            'reviewer_image': [
                {
                    'url': "http://google.com"
                },
                {
                    'url': "http://google.com"
                }
            ]
        }
    }
}
print(xmltodict.unparse(feed, pretty=True))

Result:

<?xml version="1.0" encoding="utf-8"?>
<feed>
	<reviewer_images>
		<reviewer_image>
			<url>http://google.com</url>
		</reviewer_image>
		<reviewer_image>
			<url>http://google.com</url>
		</reviewer_image>
	</reviewer_images>
</feed>

To generate multiple <foo> you have to use { "foo": […] } and not [ {"foo": …}, {"foo": …} ].

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

3 participants