I'm trying to create the following structure in a XML file using Google Apps Script:
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/">
<fields>
<field name="TestingXML">
<value>Some Testing Value</value>
</field>
<field name="Address_es_:prefill">
<value>Client Address</value>
</field>
<field name="Address1_es_:prefill">
<value>Project Address</value>
</field>
</fields>
</xfdf>
The problem comes when trying to set the Namespace prefix:
xmlns="http://ns.adobe.com/xfdf/"
Here is my code so far:
var contractInfo = [
["Customer Name_es_:prefill","ownerFullName"],
["Address_es_:prefill", "clientAddress"],
["Address_Project_es_:prefill", "projectAddress"]
];
function CreateXML(contractInfo){
//Define Namespace
var nsh = XmlService.getNamespace('http://ns.adobe.com/xfdf/');
//Create the root element and set the namespace
var root = XmlService.createElement('xfdf', nsh);
//Create the next section
var fields = XmlService.createElement('fields');
root.addContent(fields); //attach this section to the root
//Loop and create the rest of sections based on an 2D array object.
for(var m = 0; m < contractInfo.length; m++){
var child1 = XmlService.createElement('field')
.setAttribute('name', contractInfo[m][0]);
var chiled2 = XmlService.createElement('value').setText(contractInfo[m][1]);
child1.addContent(chiled2);
fields.addContent(child1);
}
var document = XmlService.createDocument(root);
//var xml = XmlService.getPrettyFormat().format(document);
Logger.log(document);
}
However, when running this code I'm getting to following error in the Logger.log
[Document: No DOCTYPE declaration, Root is [Element: <xfdf [Namespace: http://ns.adobe.com/xfdf/]/>]]
Ideally the output XML would look like this:
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/">
<fields>
<field name="TestingXML">
<value>Some Testing Value</value>
</field>
<field name="Address_es_:prefill">
<value>Client Address</value>
</field>
<field name="Address1_es_:prefill">
<value>Project Address</value>
</field>
</fields>
</xfdf>
I believe I'm missing this section at the begining:
xml version="1.0" encoding="UTF-8"
However, this was being created by itself. How can I get around this No DOCTYPE declaration?