在 python XMLBuilder 中添加带有连字符的 XML 根属性

发布于 2024-11-29 08:30:02 字数 1709 浏览 3 评论 0原文

我正在与 google checkout api 集成,它们的所有属性在其属性值中都包含连字符。因此,要创建一个对订单收费的请求,我需要发送一个 xml 帖子,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<charge-and-ship-order xmlns="http://checkout.google.com/schema/2" google-order-number="6014423719">
    <amount currency="USD">335.55</amount>
</charge-and-ship-order>

我在使用属性“google-order-number”构建该 xml 时遇到问题。如果我想创建一个空节点,则以下代码可以工作:

>>> xml=XMLBuilder()
>>> xml << ('charge-and-ship-order, {'xmlns':'xxx','google-order-number':'3433'})
>>> str(xml)
>>> <charge-and-ship-order google-order-number="3433" xmlns="xxx" /> 

但是如果我尝试使用记录的方式为金额创建子节点:

>>> xml=XMLBuilder()
>>> with xml('charge-and-ship-order', xmlns='xxx', google-order-number='3433'}):
>>>     with xml('amount', currency="USD"):
>>>         xml << '4.54'

我收到一条错误消息:

SyntaxError: keyword can't be an expression

我也尝试过:

>>> xml=XMLBuilder()
>>> with xml('charge-and-ship-order', {'xmlns':'xxx', 'google-order-number':'3433'}):
>>>     with xml << 'test'

并且我在 XMLBuilder 库中得到了一个回溯,说

    File "/xmlbuilder/xmlbuilder/__init__.py", line 102, in __call__
    x(*dt,**mp)
    File "/xmlbuilder/xmlbuilder/__init__.py", line 36, in __call__
    text = "".join(dt)
    TypeError: sequence item 0: expected string, dict found

有什么想法如何使用这样的属性吗?我正在使用位于的 XMLBuilder 库 http://pypi.python.org/pypi/xmlbuilder

I'm integrating with the google checkout api and all of their attributes include hyphens in their attribute values. So to create a request to charge an order I need to send an xml post that looks like:

<?xml version="1.0" encoding="UTF-8"?>
<charge-and-ship-order xmlns="http://checkout.google.com/schema/2" google-order-number="6014423719">
    <amount currency="USD">335.55</amount>
</charge-and-ship-order>

I'm having trouble building that xml with the attribute "google-order-number". The following code works if I want to create an empty node:

>>> xml=XMLBuilder()
>>> xml << ('charge-and-ship-order, {'xmlns':'xxx','google-order-number':'3433'})
>>> str(xml)
>>> <charge-and-ship-order google-order-number="3433" xmlns="xxx" /> 

But If I try to child node for the amount using the documented way:

>>> xml=XMLBuilder()
>>> with xml('charge-and-ship-order', xmlns='xxx', google-order-number='3433'}):
>>>     with xml('amount', currency="USD"):
>>>         xml << '4.54'

I get an error saying:

SyntaxError: keyword can't be an expression

I've also tried:

>>> xml=XMLBuilder()
>>> with xml('charge-and-ship-order', {'xmlns':'xxx', 'google-order-number':'3433'}):
>>>     with xml << 'test'

and I get a traceback in the XMLBuilder library saying

    File "/xmlbuilder/xmlbuilder/__init__.py", line 102, in __call__
    x(*dt,**mp)
    File "/xmlbuilder/xmlbuilder/__init__.py", line 36, in __call__
    text = "".join(dt)
    TypeError: sequence item 0: expected string, dict found

Any Ideas how to use an attribute like that? I'm using the XMLBuilder library located at
http://pypi.python.org/pypi/xmlbuilder

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2024-12-06 08:30:02

您可以像这样在字典中传递属性:

function_call(**{'weird-named-key': 'value'})

You can pass the attributes in a dictionary like this:

function_call(**{'weird-named-key': 'value'})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文