Reputation: 229
I've been looking for a way to parse and edit XML using Google Apps Script. Parsing the data using the in-built Xml class is easy enough but this doesn't let me edit any of the data. Take for example the example 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='"Xh9QE00OESt7I2Bp"'>
<id>http://www.google.com/m8/feeds/profiles/domain/test.com/full/user</id>
<info>Test Info</info>
</entry>
Say I'm looking to modify the info entry. Currently I'm just keeping the whole thing as a string, using indexOf("<info>")
to find where the entry starts and replacing the test from there to indexOf("</info>")
. This seems to work but I don't think it's that reliable (if the tag has an attribute it will fail to find it).
I saw another thread on here were someone suggested using XML (not Xml) to modify the attributes, but I can't figure out how to parse the existing xml I have (retrieved with UrlFetchApp into a string) into the object.
Does anyone have any suggestions on this, it would be gratefully appreciated.
Upvotes: 1
Views: 3066
Reputation: 229
In case anyone finds this in the future (hello future people, how are the flying cars and robot maids?), I wasn't able to find a way to parse and edit xml in apps script so I wrote up my own json to xml functions that worked for me (processing data from the google profile api). I haven't tested it with much else so you will probably have to modify them if you want to use them.
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;
}
Upvotes: 2