Reputation: 1964
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?
Upvotes: 1
Views: 732
Reputation: 201378
I believe your goal as follows.
You want to create the following xml data 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>
For this, how about this modification? In your script, in order to retrieve the result xml data, please use XmlService.getPrettyFormat().format(document)
. And also, there are several modification points in your script for achieving above sample value. So also, I modified them.
function CreateXML(contractInfo){
// This is a sample value. Please modify this for your actual situation.
var contractInfo = [
["TestingXML","Some Testing Value"],
["Address_es_:prefill", "Client Address"],
["Address1_es_:prefill", "Project Address"]
];
var nsh = XmlService.getNamespace('http://ns.adobe.com/xfdf/');
var root = XmlService.createElement('xfdf', nsh);
var fields = XmlService.createElement('fields', nsh); // Modified
for(var m = 0; m < contractInfo.length; m++){
var child1 = XmlService.createElement('field', nsh).setAttribute('name', contractInfo[m][0]); // Modified
var chiled2 = XmlService.createElement('value', nsh).setText(contractInfo[m][1]); // Modified
child1.addContent(chiled2);
fields.addContent(child1);
}
root.addContent(fields); // Added
var document = XmlService.createDocument(root);
var res = XmlService.getPrettyFormat().format(document); // Added
console.log(res);
}
Your sample output value is different from the value of contractInfo
in your script. So I used the following contractInfo
as the sample value.
var contractInfo = [
["TestingXML","Some Testing Value"],
["Address_es_:prefill", "Client Address"],
["Address1_es_:prefill", "Project Address"]
];
When above script is run, the sample output values as shown above section can be obtained.
Upvotes: 1