2012-08-02 98 views
4

我需要使用電子表格中的特定數據生成XML文檔。如何生成XML?

我使用GoogleScript,但我沒有看到任何資料庫,幫助我生成XML。 (標準的XML服務只解析XML)

上午我被迫手要做到這一點還是我失去了一些東西?

你能推薦任何可能有助於生成XML的庫或Javascript函數嗎?

感謝

回答

0

其粘貼在一個excel等,然後出口。

否則使用Apache的C-Xerces

6

您可以使用HtmlService模板構建XML文檔。你可以也可以通過從一個空文檔(Xml.parse('')或類似的東西)開始用Xml服務創建文檔,然後用Xml.element()等操作它,但我懷疑第一個想法要容易得多。

4

最近我發現使用存在於GAS中的XML類(而不是Xml)生成XML的能力。一個例子是以下

function makeXML() { 
    var xml = new XML('<a/>'); 
    xml['@attrib1'] = 'atribValue'; 
    xml.data = 'dataValue'; 
    xml.data.sub_data = 'subDataValue'; 
    return xml.toString(); 
} 

function doGet(e) { 
    var app = UiApp.createApplication(); 
    app.add(app.createLabel(makeXML())); 
    return app; 
} 

此腳本的輸出是<a attrib1="atribValue"> <data> dataValue <sub_data>subDataValue</sub_data> </data> </a>

0

According to Google適當方式做到這一點是使用parseJS一組嵌套的JavaScript數組的變換成的XML。

要創建可經由XmlDocument.toXmlString(被序列爲字符串 表示)一個如下實例,使用該方法 parseJS(jsObject),它建立從一組嵌套 JavaScript數組的XML文檔。

該網頁上的鏈接是壞的,但you can find the documentation for parseJS here.

我不喜歡這種做法,但我覺得他們的XML模塊上的文檔是窮人的創作方面,所以這是一個我跟着去了。在我的情況下,我需要將每行映射到一個元素,其中一些片段是子元素,一些是屬性。這可以寫得更簡單,但我不確定它會更容易理解。至少不是我。

所以這是我想出了我的需求,這就造成了頂部封閉XML,然後通過節點將節點的解決方案。一旦完成,它會打開一個匹配的Google文檔並將渲染的XML放入其中。有一些非常基本的檢查,以確保我有最低限度所需的單元格,所以如果我的一個用戶放入一個壞行,我只是跳過它。這也讓我沒有在底部擔心多餘的空行耍着等

var now = new Date(); 
    var UTCString = ''; 
    UTCString = UTCString.concat(now.getFullYear(),'-',now.getMonth()+1,'-',now.getDate(),'T',now.getHours(),':',now.getMinutes(),':',now.getSeconds(),'+00:00'); 

    var fullDoc = ["entities", {"type":"federal-body"}, {"updated":UTCString}]; 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getSheets()[0]; 

    // This selects ALL the data w/o having to specify a range 
    var range = sheet.getDataRange(); 
    var values = range.getValues(); 

    // This logs the spreadsheet in CSV format with a trailing comma 
    for (var i in values) { 
    //not elegant but worth it for the get-all-range 
    if (i==0) 
     continue; 
    if (values[i][0] && values[i][2]) { 
     //make an object if column 1 is empty 
     //column 0 is the id attribute 
     //column 1, if present, is the parent-id attribute 
     if (values[i][1]) { 
     var node = ["entity", {"id": values[i][0]}, {"parent-id": values[i][1]} ];  
     } else { 
     var node = ["entity", {"id": values[i][0]} ]; 
     } 
     //column 2 is the name value and has the attribute of role:official 
     node.push([ "name", {"role" : "official"}, values[i][2] ]); 
     //column 3, if present, is the abbr value 
     if (values[i][3]) { 
     node.push([ "abbr", values[i][3] ]); 
     } 
     //column 4, if present, is a name value with role:historical 
     if (values[i][4]) { 
     node.push([ "name", {"role" : "historical"}, values[i][3] ]); 
     } 
     //column 5, if present, is a name value with role:leadership 
     if (values[i][5]) { 
     node.push([ "name", {"role" : "leadership"}, values[i][5]]); 
     }  
     fullDoc.push(node); 
    } 
    } 

    var xml = Xml.parseJS(fullDoc); 

    //put it in the file 
    var doc = DocumentApp.openByUrl('https://docs.google.com/document/d/17WNYtvjCgR-w3m3tIFNmHlr3aVLqfUaobGqFu-hIGks/edit'); 
    var header = doc.getHeader(); 
    if (header) 
    header.clear(); 
    var footer = doc.getFooter(); 
    if (footer) 
    footer.clear(); 
    var body = doc.getBody(); 
    body.setText(xml.toXmlString()); 

我已經投入到一個菜單項,在此需要手動運行,但最終我會考慮的那一刻在保存時啓動它。輸出XML是未格式化的,但是因爲我只是將它推入數據庫(我打算稍後自動化的更新過程),我不在乎。

對於我來說,使用屬性的列標題會更聰明,但是我不想混淆來自客戶端的源文檔。

你可以看到the source spreadsheet以及the output作比較。