как поместить xml в couchDB?

я хочу сделать это: 1. ПОМЕСТИТЕ строку xml в couchdb сервер. что-то как:

curl -X PUT  http://localhost:5984/db/_design/app/_update/echo/h1
-d "<doc><name1>value1</name1><name2>value2</name2></doc>"
  1. в couchdb стороне сервера я анализирую строку xml в объект json.
  2. сохраните объект json как документ.

действительно ли это возможно? как я, как предполагается, делаю?

спасибо!

11
задан turtledove 9 August 2010 в 06:49
поделиться

2 ответа

Your best bet is going to be to covert the XML into JSON before you send it to CouchDB. Of course, you could also not convert it and just store it in a JSON field. Your document might look something like this:

{
  "_id": "...",
  "_rev": "...",
  "xml": "<doc><name1>value1</name1><name2>value2</name2></doc>",
  ...some other fields...
}

You could also store the XML as an attachment: http://wiki.apache.org/couchdb/HTTP_Document_API#Attachments That way you can make a call to /dbName/documentID/storedData.xml or whatever, and get the file back with the proper XML Content-type.

It really depends on whether you want to get XML back, or whether you want to only work with the JSON after the conversion.

5
ответ дан 3 December 2019 в 09:39
поделиться

Я нашел другой способ сделать это, вот пример:

  1. create db

    curl -X PUT http: // localhost: 5984 / bookstore

  2. create проектный документ

    curl -X POST http: // localhost: 5984 / bookstore / _bulk_docs -d @ design.doc

где содержимое design.doc:

{"docs":
  [
    {
        "_id": "_design/app",
        "updates": {
            "xml2json": "
              function (doc, req) {
                if(req.query.doc == null) {
                  return [null, \"doc is null!\\n\"];
                }
                var xmlDoc = req.query.doc.replace(/^<\?xml\s+version\s*=\s*([\"'])[^\1]+\1[^?]*\?>/, \"\");
                var html = new XML(xmlDoc);
                if(doc==null) {
                  doc = {};
                  doc._id=html.BookList.BookData.@isbn13.toString();
                  if(doc._id==null||doc._id==\"\") {
                    doc._id=html.BookList.BookData.@isbn.toString();
                  }
                }
                if (doc._id == null || doc._id == \"\") {
                  return [null, \"doc id is null!\\n\"];;
                }
                doc.title = html.BookList.BookData.Title.text();
                doc.longtitle = html.BookList.BookData.TitleLong.text();
                doc.authors = html.BookList.BookData.AuthorsText.text();
                doc.publisher = html.BookList.BookData.PublisherText.text();
                return [doc, \"ok!\\n\"];
              }"
        }
    }
  ]
}
  1. test _update

    doc = $ (cat isbndb.sample); doc = "$ (perl -MURI :: Escape -e 'print uri_escape ($ ARGV [0]);'" $ doc ")"; curl -X PUT http: // localhost: 5984 / bookstore / _design / app / _update / xml2json / 9781935182320? doc = "$ doc"

где содержимое isbndb.sample:

<?xml version="1.0" encoding="UTF-8"?>

<ISBNdb server_time="2010-08-11T04:13:08Z">
<BookList total_results="1" page_size="10" page_number="1" shown_results="1">
<BookData book_id="mastering_perl" isbn="0596527241" isbn13="9780596527242">
<Title>Mastering Perl</Title>
<TitleLong></TitleLong>
<AuthorsText>brian d foylt;/AuthorsText>
<PublisherText publisher_id="oreilly_media">Sebastopol, CA : O'Reilly Media, c2007.</PublisherText>
</BookData>
</BookList>
</ISBNdb>
6
ответ дан 3 December 2019 в 09:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: