Skip to content
matecsaj edited this page Jun 13, 2020 · 2 revisions

The request dictionary can be quite mystical as to how it converts the dictionary to XML. Here I'll lay out a few examples to illustrate the primary use cases. If you're ever unsure what the XML will be, you can import the dict2xml function and try it out.

from ebaysdk.utils import dict2xml
print(dict2xml({'a': 'b'})

1) Simple One tag

resp = api.execute('MyVerb', {'a': 'b'})

XML

<a>b</a>

2) One tag with attributes

dict2 = {
  'tag': {
      '#text': 222,
      '@attrs': {'site': 'US', 'attr2': 'attr2value'}
  }
}
resp = api.execute('MyVerb', dict2)  

XML

<tag attr2="attr2value" site="US">222</tag>

3) Repeating tags

dict3 = {
  'itemFilter': [
      {'name': 'Condition', 'value': 'Used'},
      {'name': 'LocatedIn', 'value': 'GB'},
      {'name': 'More', 'value': 'more'},
  ]
}
resp = api.execute('MyVerb', dict3)

XML

<itemFilter>
  <name>Condition</name>
  <value>Used</value>
</itemFilter>
<itemFilter>
  <name>LocatedIn</name>
  <value>GB</value>
</itemFilter>
<itemFilter>
  <name>More</name>
  <value>more</value>
</itemFilter>

4) Nested tags and attributes

dict5 = {
  'tag1': {
      '#text': {'tag2': {
          '#text': 'tag2 value',
          '@attrs': {'tag2attr': 'myvalue'}
      }},
      '@attrs': {'site': 'US', 'tag1attr': 'myvalue'}
  }
}
resp = api.execute('MyVerb', dict5)

XML

<tag1 site="US" tag1attr="myvalue">
  <tag2 tag2attr="myvalue">tag2 value</tag2>
</tag1>
Clone this wiki locally