buildPostingBody static method
Implementation
static Future<String?> buildPostingBody(List<IFormField>? fields, {String rootname = "FORM"}) async
{
try
{
// build xml document
XmlDocument document = XmlDocument();
XmlElement root = XmlElement(XmlName(S.isNullOrEmpty(rootname) ? "FORM" : rootname));
document.children.add(root);
if (fields != null)
fields.forEach((field)
{
// postable?
if (field.postable == true)
{
if (field.values != null)
{
field.values!.forEach((value)
{
XmlElement node;
String name = field.field ?? field.id ?? "";
try
{
// valid element name?
if (!S.isNumber(name.substring(0,1)))
{
node = XmlElement(XmlName(name));
}
else
{
node = XmlElement(XmlName("FIELD"));
node.attributes.add(XmlAttribute(XmlName('id'), name));
}
}
catch(e)
{
node = XmlElement(XmlName("FIELD"));
node.attributes.add(XmlAttribute(XmlName('id'), name));
}
// add field type
if (!S.isNullOrEmpty(field.elementName)) node.attributes.add(XmlAttribute(XmlName('type'), field.elementName));
/// GeoCode for each [iFormField] which is set on answer
if (field.geocode != null) field.geocode!.serialize(node);
// add meta data
if (!S.isNullOrEmpty(field.meta)) node.attributes.add(XmlAttribute(XmlName('meta'), field.meta));
// value
try
{
// Xml Data
if ((field is InputModel) && (field.format == InputFormats.xml))
{
var document = XmlDocument.parse(value);
var e = document.rootElement;
document.children.remove(e);
node.children.add(e);
}
// Non-XML? Wrap in CDATA
else if (Xml.hasIllegalCharacters(value)) node.children.add(XmlCDATA(value));
// Normal Text
else node.children.add(XmlText(value));
}
on XmlException catch(e)
{
node.children.add(XmlCDATA(e.message));
}
// Add Node
root.children.add(node);
});
}
else
{
// Build Element
XmlElement node;
String name = field.field ?? field.id ?? "";
try
{
// Valid Element Name
if (!S.isNumber(name.substring(0,1)))
{
node = XmlElement(XmlName(name));
}
// In-Valid Element Name
else
{
node = XmlElement(XmlName("FIELD"));
node.attributes.add(XmlAttribute(XmlName('id'), name));
}
}
catch(e)
{
// In-Valid Element Name
node = XmlElement(XmlName("FIELD"));
node.attributes.add(XmlAttribute(XmlName('id'), name));
}
// Add Field Type
if (!S.isNullOrEmpty(field.elementName)) node.attributes.add(XmlAttribute(XmlName('type'), field.elementName));
// Add Node
root.children.add(node);
}
}
});
// Set Body
return document.toXmlString(pretty: true);
}
catch(e)
{
Log().error("Error serializing posting document. Error is ${e.toString()}");
return null;
}
}