Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:01:40+00:00 2026-06-03T14:01:40+00:00

I’m trying to build a library that will build XML responses for an API.

  • 0

I’m trying to build a library that will build XML responses for an API. To help illustrate my problem, here are 2 sample API responses. the first for displaying menus, the second to display text.

<CiscoIPPhoneMenu>
  <Title>Title text goes here</Title>
  <Prompt>Prompt text goes here</Prompt>
  <MenuItem>
   <Name>The name of each menu item</Name>
   <URL>The URL associated with the menu item</URL>
  </MenuItem>
  <SoftKeyItem>
   <Name>Name of soft key</Name>
   <URL>URL or URI of soft key</URL>
   <Position>Position information of the soft key</Position>
  </SoftKeyItem>
</CiscoIPPhoneMenu>

…

<CiscoIPPhoneText>
  <Title>Title text goes here</Title>
  <Prompt>The prompt text goes here</Prompt>
  <Text>The text to be displayed as the message body goes here</Text>
  <SoftKeyItem>
   <Name>Name of soft key</Name>
   <URL>URL or URI of soft key</URL>
   <Position>Position information of the soft key</Position>
  <SoftKeyItem>
</CiscoIPPhoneText>

Okay, so my module outline looks like this:

class CiscoIPPhone(object): 
    def __init__(self, title=None, prompt=None):
        self.title = title
        self.prompt = prompt

class MenuItem(object):
    def __init__(self, name, url):
        self.name = name
        self.url = url

class CiscoIPPhoneMenu(CiscoIPPhone):
    def __init__(self, *args, **kwargs):
        super(CiscoIPPhoneMenu, self).__init__(*args, **kwargs)
        self.items = []

    def add_menu(self, name, url):
        self.items.append(MenuItem(name, url))

Note: for readability I removed the validation and sanitization these classes handle.

So my questions are:

  1. I’m practically outputting a serialized representation of these objects, is doing it this considered wrong or bad practice?
  2. Is there an design pattern that describes this kind of API interface class?
  3. Is there a Python library that is elegantly written (Pythonic) that does something similar? (I’m thinking like a stripped down version of Django’s model serialization, or Django-Tastypie).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:01:43+00:00Added an answer on June 3, 2026 at 2:01 pm

    Most unfortunately, I cannot comment on the Python side of things.

    Personally, I think this design is very acceptable.

    There is one place where these messages are serialized and deserialized. The domain code using these classes creates one, fills it with the necessary data, and hands it (or its serialized representation) down to another component. When the actual serialization happens, the class itself checks whether all mandatory data has been set.

    The classes are test-friendly. You just have to create a message, fill in some values, and check the XML-serialized version. The tests schema-check both the expectation and the actual output. These tests are the specification of the entire protocol.

    The design lends itself to a nice fluent API:

    new CiscoIPPhoneMenu()
        .withTitle("Title text goes here")
        .withPrompt("Prompt text goes here")
        ...
    

    For reference purposes, Martin Reddy states in API design for C++ that, for a protocol or file format, to have one component that translates to or from the serialized representation. I think this is exactly the purpose of these classes.

    If the serialized representation changes (e.g. from an XML format to a binary one), you are easily able to switch representation by e.g. providing a second ctor that accepts a format, or you anticipate that change and introduce a SerializationFormat enum.

    I can readily imagine a python module that takes a XML schema ComplexType and generates the matching python class from it. Once I’ve tried to generate those classes in C# using Altova XML Spy; however this involved referencing a runtime DLL, and using another DLL just for this seemed too much overhead. This is essentially the way protobuf works, with the XML schema ComplexType being defined in an external DSL, and the serialization format a binary one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.