2013-02-08 2 views
1

Google Apps Script를 사용하여 XML을 구문 분석하고 편집하는 방법을 찾고있었습니다. 내장 된 Xml 클래스를 사용하여 데이터를 파싱하기는 쉽지만 데이터를 편집 할 수는 없습니다. 예를 들어 XML을 보자.Google Apps Script로 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

(안녕하세요 미래의 사람? 비행 자동차와 로봇 하녀 얼마나), I 스크립트 그래서 내가 쓴 애플 리케이션에 구문 분석하는 방법과 XML 편집을 찾을 수 없습니다가 나를 위해 일한 XML 함수에 대한 내 자신의 json (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 로봇 하녀 용! 'Utilities.jsonStringify()'는'xmlToJson()'의 작업을하지 않겠습니까? – Mogsdad

+0

"TypeError : 개체 XmlDocument에서 getName 함수를 찾을 수 없습니다." 두 번째 줄에 – Snowball

관련 문제