2010-08-09 84 views
9

我想這樣做: 1.將一個xml字符串傳遞給couchdb服務器。類似:如何將xml放入couchDB?

curl -X PUT http://localhost:5984/db/_design/app/_update/echo/h1 
-d "<doc><name1>value1</name1><name2>value2</name2></doc>" 
  • 在CouchDB的服務器端,我解析的XML字符串成JSON對象。
  • 將json對象保存爲文檔。
  • 這是可能的嗎?我該怎麼辦?

    謝謝!

    回答

    5

    最好的辦法是將XML轉換爲JSON,然後再將其發送到CouchDB。當然,你也可以不轉換它,只是將其存儲在JSON字段中。您的文檔可能是這個樣子:

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

    您還可以存儲XML作爲附件:http://wiki.apache.org/couchdb/HTTP_Document_API#Attachments這樣就可以使/dbName/documentID/storedData.xml一個電話或什麼的,並得到迴文件與適當的XML內容類型。

    這實際上取決於您是要恢復XML,還是隻想在轉換後使用JSON。

    +0

    我想獲得json。所以附件是不合適的,也許最好的方法是使用json的領域來將xml傳輸到服務器,但是當我的xml包含如此多的'''字符時很不方便,他們必須手動轉義出來 – turtledove 2010-08-10 02:24:00

    +1

    你應該不需要轉義只需將你的XML轉換成一個字符串,它看起來就像它一樣,然後運行json_encode(),它將爲你處理所有的轉義和格式化,並且你總是可以把XML轉換成PHP對象(相當簡單的操作),然後通過json_encode()運行它並將其存儲在文檔中。 – 2010-08-10 17:22:29

    6

    我找到了另一種方式來做到這一點,這裏是樣本:

    1. 創建數據庫

      捲曲-X PUT http://localhost:5984/bookstore

    2. 設計文檔

      捲曲-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 = {}; 
            [email protected](); 
            if(doc._id==null||doc._id==\"\") { 
            [email protected](); 
            } 
           } 
           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. 測試_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>