2012-08-02 95 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作比较。