TommyK
TommyK

Reputation: 416

CouchDB Return data as XML

I am trying to work with CouchDB and I am pretty new to it.

I have data stored and I want to query it and return the data in XML form.

Do I write a view or a show function? Where does one write a show function on futon?

Does anybody have any examples?

Upvotes: 2

Views: 993

Answers (1)

javabeangrinder
javabeangrinder

Reputation: 7159

If you don't want to use CouchApp and just want to get your views as xml you can use this one liner (-ish) list:

"xml": "function element(name, row){send('<' + name + '>\\n');   for(field in row) {  if (typeof(row[field])=='object') {element(field, row[field])} else {send('<field name=\"' + field + '\">');  send((''+row[field]).replace(/&/g,'&amp;').replace(/</g,'&lt;'));  send('</field>\\n');}  }   send('</' + name + '>\\n');};        function(head, req) {    provides('xml', function() {    send('<xml>\\n');    while (row=getRow()) {   element('row', row)   }    send('</xml>');   });    }"

I've kept it as one line since it has to be entered as a one line function in the design document.

Create a design document

If you not yet have a design document the easiest way to create one is to create a view in futons Temporary View and [ Save As...] design_document_name of your choice and a view_name of your choice. I'll hereby use the design document "select" and a view called "by_id". My by_id view looks like this:

function(doc) {
  emit(doc._id, null);
}

Enter the list into the design document

When this has been created select View "Design documents" from futon's drop down menu. Click on the newly (perhaps) created design document _design/select.

Select the tab Source and double-click on the Source. Click again just before "views":... and make sure you caret is placed there.

Make a new line above that row and enter:

"lists": {
    |
}

In the middle of the curly braces (where the pipe character is) paste the one-liner from above. Save the document.

Use the new list and get XML

Now you can get any data from any view as XML. Please note that you only can use views and not the entire database content directly, even if my view does just that.

To view your data use this URL in your browser:

http://localhost:5984/content/_design/select/_list/xml/by_id?include_docs=true&limit=10
  • The parameter include_docs=true makes each document part of the result list. This is a standard CouchDB feature.
  • The parameter limit=10 keeps the result list at maximum 10 documents to limit the time it takes if you have a large database.
  • The parameter reduce=false can be entered as well if you have a reduce function in your view.

How it looks at my computer

I have a database that looks like this when I use the view directly:

url=localhost:5984/cartoons/_design/select/_view/by_id?include_docs=true

{"total_rows":6,"offset":0,"rows":[
{"id":"Batman","key":"Batman","value":null,"doc":{"_id":"Batman","_rev":"1-0ec5a1032c2fa8486c921924e13a31cf","publisher":"DC Comics"}},
{"id":"Donald Duck","key":"Donald Duck","value":null,"doc":{"_id":"Donald Duck","_rev":"1-1c431dfb2c46991ec999743830a5363b","publisher":"Walt Disney"}},
{"id":"Iron Man","key":"Iron Man","value":null,"doc":{"_id":"Iron Man","_rev":"1-4f3a3d862b3601a74d6277f4be930e33","publisher":"Marvel Comics"}},
{"id":"Mickey Mouse","key":"Mickey Mouse","value":null,"doc":{"_id":"Mickey Mouse","_rev":"1-1c431dfb2c46991ec999743830a5363b","publisher":"Walt Disney"}},
{"id":"Spider-Man","key":"Spider-Man","value":null,"doc":{"_id":"Spider-Man","_rev":"1-4f3a3d862b3601a74d6277f4be930e33","publisher":"Marvel Comics"}},
{"id":"Superman","key":"Superman","value":null,"doc":{"_id":"Superman","_rev":"1-0ec5a1032c2fa8486c921924e13a31cf","publisher":"DC Comics"}}
]}

And in XML

url=localhost:5984/cartoons/_design/select/_list/xml/by_id?include_docs=true

<xml>
    <row>
        <field name="id">Batman</field>
        <field name="key">Batman</field>
        <value>
        </value>
        <doc>
            <field name="_id">Batman</field>
            <field name="_rev">1-0ec5a1032c2fa8486c921924e13a31cf</field>
            <field name="publisher">DC Comics</field>
        </doc>
    </row>
    <row>
        <field name="id">Donald Duck</field>
        <field name="key">Donald Duck</field>
        <value>
        </value>
        <doc>
            <field name="_id">Donald Duck</field>
            <field name="_rev">1-1c431dfb2c46991ec999743830a5363b</field>
            <field name="publisher">Walt Disney</field>
        </doc>
    </row>
    <row>
        <field name="id">Iron Man</field>
        <field name="key">Iron Man</field>
        <value>
        </value>
        <doc>
            <field name="_id">Iron Man</field>
            <field name="_rev">1-4f3a3d862b3601a74d6277f4be930e33</field>
            <field name="publisher">Marvel Comics</field>
        </doc>
    </row>
    <row>
        <field name="id">Mickey Mouse</field>
        <field name="key">Mickey Mouse</field>
        <value>
        </value>
        <doc>
            <field name="_id">Mickey Mouse</field>
            <field name="_rev">1-1c431dfb2c46991ec999743830a5363b</field>
            <field name="publisher">Walt Disney</field>
        </doc>
    </row>
    <row>
        <field name="id">Spider-Man</field>
        <field name="key">Spider-Man</field>
        <value>
        </value>
        <doc>
            <field name="_id">Spider-Man</field>
            <field name="_rev">1-4f3a3d862b3601a74d6277f4be930e33</field>
            <field name="publisher">Marvel Comics</field>
        </doc>
    </row>
    <row>
        <field name="id">Superman</field>
        <field name="key">Superman</field>
        <value>
        </value>
        <doc>
            <field name="_id">Superman</field>
            <field name="_rev">1-0ec5a1032c2fa8486c921924e13a31cf</field>
            <field name="publisher">DC Comics</field>
        </doc>
    </row>
</xml>

Upvotes: 1

Related Questions