2013-02-08 50 views
1

我一直在尋找一種使用Google Apps腳本解析和編輯XML的方法。使用內置的Xml類解析數據很容易,但這不會讓我編輯任何數據。以示例XML爲例:使用Google Apps腳本解析和編輯XML

<?xml version='1.0' encoding='UTF-8'?> 
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='&quot;Xh9QE00OESt7I2Bp&quot;'> 
<id>http://www.google.com/m8/feeds/profiles/domain/test.com/full/user</id> 
<info>Test Info</info> 
</entry> 

說我正在修改信息條目。目前,我只是將整個事情保持爲一個字符串,使用indexOf("<info>")來查找條目的起始位置,並將測試從那裏更換爲indexOf("</info>")。這似乎工作,但我不認爲這是可靠的(如果標籤有一個屬性,它將無法找到它)。

我看到另一個線程在這裏有人建議使用XML(而不是Xml)來修改屬性,但我無法弄清楚如何解析現有的XML(用UrlFetchApp檢索到字符串中)到對象中。

有沒有人對此有任何建議,將不勝感激。

回答

2

如果任何人發現這在未來(你好未來的人,是如何飛行的汽車和機器人女僕?),我沒能找到一種方法來分析和編輯XML應用腳本,所以我寫了將我自己的json轉換爲適用於我的xml函數(處理來自Google配置文件API的數據)。我還沒有測試過它,所以如果你想使用它們,你可能需要修改它們。

function xmlToJson(xmlElement) { 
    var e = {"namespace" : xmlElement.getName().getNamespace(), 
      "name" : xmlElement.getName().getLocalName()}; 
    var xmlAs = xmlElement.getAttributes(); 
    if(xmlAs.length > 0) { 
    e.attributes = {}; 
    for(var j = 0; j < xmlAs.length; j++) { 
     e.attributes[xmlAs[j].getName().getLocalName()] = {"namespace" : xmlAs[j].getName().getNamespace(), 
                 "name" : xmlAs[j].getName().getLocalName(), 
                 "value" : xmlAs[j].getValue()}; 
    } 
    } 

    var xmlChildren = xmlElement.getElements(); 
    if(xmlChildren.length > 0) { 
    e.children = {}; 
    for(var i = 0; i < xmlChildren.length; i++){ 
     var child = xmlToJson(xmlChildren[i]); 
     if(typeof e.children[child.name] != "undefined") 
     e.children[child.name].push(child); 
     else 
     e.children[child.name] = [child]; 
    } 
    } else { 
    e.value = xmlElement.getText(); 
    } 
    return e; 
} 

function jsonToXmlString(json) { 
    var xml = "<?xml version='1.0' encoding='UTF-8'?>"; 
    var namespaces = new Object(); // List of things which are possibly namespaces 
    namespaces["http://www.w3.org/2000/xmlns/"] = "xmlns"; 

    function appendNode(node) { 
    if(typeof node.attributes != 0) { 
     var attributes = ""; // Get attributes first incase any are namespaces 
     var keys = getKeys(node.attributes); 
     for(var i = 0; i < keys.length; i++) { // Loop through attributes once to get namespaces 
     if(node.attributes[keys[i]].value.indexOf("http") == 0) // Possible namespace, store in namespaces 
      namespaces[node.attributes[keys[i]].value] = node.attributes[keys[i]].name; 
     } 
     // If we only do one loop, there may be some namespaces on attributes that don't get recorded first 
     for(var i = 0; i < keys.length; i++) { 
     if(node.attributes[keys[i]].namespace != "") // Get namespace if needed 
      var ns = (namespaces[node.attributes[keys[i]].namespace] || node.attributes[keys[i]].namespace) + ":"; 
     else 
      var ns = ""; 
     attributes += " " + ns + node.attributes[keys[i]].name + "='" + node.attributes[keys[i]].value + "'"; 
     } 
    } 
    if(node.namespace != "") // Get namespace if needed 
     var ns = (namespaces[node.namespace] || node.namespace) + ":"; 
    else 
     var ns = ""; 

    xml += "<" + ns + node.name + attributes; 

    if(typeof node.children != "undefined") { 
     xml += ">"; 
     var cKeys = getKeys(node.children); 
     for(var i = 0; i < cKeys.length; i++) { 
     for(var j = 0; j < node.children[cKeys[i]].length; j++) 
      appendNode(node.children[cKeys[i]][j]); 
     } 
    } else if(typeof node.value != "undefined") { 
     xml += ">" + node.value; 
    } else { 
     xml += "/>"; 
     return 
    } 

    xml += "</" + ns + node.name + ">" 
    } 

    appendNode(json); 
    return xml; 
} 
+0

+1 for robot maids! 'Utilities.jsonStringify()'不會執行'xmlToJson()'的工作嗎? – Mogsdad 2013-05-29 04:47:31

+0

我收到「TypeError:Can not find function getName in object XmlDocument。」在第二行 – Snowball 2016-05-29 00:53:43