/* Test_XML-read-write.jsx ====== UTF-8 =========================================================
Tests about Self-clsoing xml-tags
Comment Does not need FM to be active
History 2021-03-19
*/ ; // =====================================================================================
var KLD_Z = KLD_Z || {}; // global script object
KLD_Z.GetXMLdata = function (sXmlFile) { // ---------------------------------------------------
/* Read localisation strings from xml file
Argements sXmlFile fully qualified path of xml file or just the file name
If no filepath is given (no slash or backslash present),
then the file is assumed in the same directory as the script
Returns xml sructure
Usage xSettings = GetXMLdata (sXmlFile);
Reference E:\FM-ExtendScript\SaveBookOldVersion\SaveBookAsOldVersion.jsx by Rick Quatro
History 2020-11-11
*/
var fXmlFile, xData, index;
index = sXmlFile.indexOf("/") ;
index = sXmlFile.indexOf("\\");
if ((sXmlFile == null) || (sXmlFile == undefined)) {
return false;
} else if (sXmlFile.indexOf("/") > 0 || sXmlFile.indexOf("\\") > 0) { // fully qualified path
fXmlFile = new File(sXmlFile);
} else {
fXmlFile = new File($.fileName.replace (/[^\\\/]+$/i , sXmlFile)); // file in script folder
}
if (fXmlFile.exists === false) {
alert ("XML file «" + sXmlFile + "» not found", "GetXMLdata", true);
return false;
}
fXmlFile.open("r");
try {
xData = new XML(fXmlFile.read());
fXmlFile.close();
return xData;
} catch (e) { // what kind of error can this be ?
alert ("XML file «" + sXmlFile + "» Read error:\n" + e, "GetXMLdata", true);
fXmlFile.close();
return false;
}
} //--- end GetXMLdata
KLD_Z.WriteXMLdata = function (xmlData, sXmlFile) { // ============================================
/* Write xml data to new file
Argements xmlData the full structure to be written.
sXmlFile fully qualified path of xml file or just the file name
If no filepath is given (no slash or backslash present),
then the file is assumed in the same directory as the script.
The file is created anew in UTF-8 with BOM
Reference https://www.davidebarranca.com/2013/03/presets-management-with-dropdownlist-and-xml-in-extendscript/
Klaus Göbel in https://community.adobe.com/t5/framemaker/xml-and-unicode-a-quarrelling-couple/td-p/11677704
History 2020-12-14
*/
var BOM = "\uFEFF", fXmlFile;
if ((sXmlFile == null) || (sXmlFile == undefined)) {
return false;
} else if (sXmlFile.indexOf("/") > 0 || sXmlFile.indexOf("\\") > 0) { // fully qualified path
fXmlFile = new File(sXmlFile);
} else {
fXmlFile = new File($.fileName.replace (/[^\\\/]+$/i , sXmlFile)); // file in script folder
}
try {
fXmlFile.open('w');
fXmlFile.encoding = "UTF-8";
fXmlFile.write(BOM);
fXmlFile.write(xmlData);
fXmlFile.close();
} catch (e) {
alert("Error writing the XML file " + sXmlFile + "\n" + e.message);
// KLD_F.Message ("E", KLD_F.UItxt.WriteXMLdata.@msg_01, "WriteXMLdata", sXmlFile, e.message);
return false;
}
return true;
} // --- End WriteXMLdata -----------------------------------------------------------------------------
KLD_Z.main = function () {
var nItems, xData1 = {}, xData2 = {}, xData3 = {}, sA, sB, sC, sD, sE;
var sXMLfile = "C:/Users/klaus/Desktop/";
xData1 =
- ""
- " "
- " "
KLD_Z.WriteXMLdata (xData1, sXMLfile + "data1.xml");
/* file data1.xml
- ""
- " "
- " "
*/
//$.bp(true);
// ----------------------------------------------
xData2 = KLD_Z.GetXMLdata (sXMLfile + "data1.xml"); // xData2 is type xml
if (!xData2) {
alert ("Problem reading the xml data");
}
sA = xData2.item[0].toString();
$.writeln ("sA = «" + sA + "»"); // sA = «»
sB = xData2.item[1].toString();
$.writeln ("sB = «" + sB + "»"); // sB = «»
sC = xData2.item[2].toString();
$.writeln ("sC = «" + sC + "»"); // sC = «""»
sD = xData2.item[3].toString();
$.writeln ("sD = «" + sD + "»"); // sD = «" "»
sE = xData2.item[4].toString();
$.writeln ("sE = «" + sE + "»"); // sE = «" "»
// -----------------------------------------------
//$.bp(true);
xData2.item[0] = null;
xData2.item[1] = undefined;
xData2.item[2] = "";
xData2.item[3] = " ";
xData2.item[4] = " ";
KLD_Z.WriteXMLdata (xData2, sXMLfile + "data2.xml");
/* file data2.xml
-
-
*/
xData3 = KLD_Z.GetXMLdata (sXMLfile + "data2.xml");
if (!xData3) {
alert ("Problem reading the xml data"); // xdata3 is type xml
}
sA = xData3.item[0];
$.writeln ("sA = «" + sA.toString() + "» " + typeof sA ); // sA = «» xml
sB = xData3.item[1].toString();
$.writeln ("sB = «" + sB.toString() + "» " + typeof sB ); // sB = «» string
sC = xData3.item[2].toString();
$.writeln ("sC = «" + sC.toString() + "» " + typeof sC ); // sC = «» string
sD = xData3.item[3].toString();
$.writeln ("sD = «" + sD.toString() + "» " + typeof sD ); // sD= «» string
sE = xData3.item[4].toString();
$.writeln ("sE = «" + sE.toString() + "» " + typeof sE ); // sE = «» string
KLD_Z.WriteXMLdata (xData3, sXMLfile + "data3.xml");
var j, nData = xData3.item.length();
for (j=0; j < nData; j++) {
xData3.item[j] = xData3.item[j].toString();
}
KLD_Z.WriteXMLdata (xData3, sXMLfile + "data4.xml");
/* file data4.xml
*/
$.bp(true);
} //--- end main
KLD_Z.main ();