Mein xml file enthält etliche kommentare - da das file ja schliesslich mit jedem beliebigen text editor angesehen werden kann.
Wenn ich nun elemente, oder attribute ändere, gehen beim hereinlesen die kommentare flöten und beim schreiben der neuen struktur sind sie dann futsch.
Wie kann ich das verhindern? Es gibt da so einen paramter
- habe aber keine ahnung, wie der untergebracht werden muss.
Hier mein script, das alles macht, was es soll, doch eben ohne die kommentare zu bewahren:
// WriteXml-b.jsx
// https://www.davidebarranca.com/2013/03/presets-management-with-dropdownlist-and-xml-in-extendscript/
// JavaScript Tools Guide: Modifying XML elements and attributes
#target framemaker
main ();
function main () {
var nItems;
var oReaderSettings, xmlData, xmlData1, sXmlFile = "WriteXml.xml"; // file exists in same dir as this script;
xmlData1 =
<item>
<name>This shall be a secondand further elements</name>
<info>The attributes must be filled with js data.</info>
<options searchMode="Wildcards" word="1" case="0" back="0" clone="0"/>
<findtype>Text:</findtype>
<findstring>*something</findstring>
<replmode>To Text:</replmode>
<replstring>$1\t</replstring>
</item>
/* ---------------------------------------------- this does not work at all
$.bp(true);
oReaderSettings = xmlData.defaultSettings (); // xml reader settings ?
oReaderSettings.ignoreComments = false; // are they keypt ?
xmlData.setSettings (oReaderSettings); // are these the new settings?
*/
xmlData = GetXMLdata (sXmlFile); // the file already contains the root and two items
// ---------------------------------------------- add a complete item
nItems = xmlData.item.length();
xmlData.item[nItems+1] = xmlData1;
WriteXML (xmlData, sXmlFile); // write XML object to update the file
// ---------------------------------------------- modify the first item
xmlData.item[0].info = "This has been modified";
WriteXML (xmlData, sXmlFile); // write XML object to update the file
// ---------------------------------------------- modify options in the second item
xmlData.item[1].options.@searchMode = "RegEx";
xmlData.item[1].options.@word = "0";
WriteXML (xmlData, sXmlFile); // write XML object to update the file
// ---------------------------------------------- Delete the last item from the file
$.bp(true);
nItems = xmlData.item.length();
delete xmlData.item[nItems-1];
WriteXML (xmlData, sXmlFile); // write XML object to update the file
} // --- End main --------------------------------
function WriteXML (xmlData, sXmlFile) {
var fXmlFile;
fXmlFile = new File($.fileName.replace (/[^\\\/]+$/i , sXmlFile));
try {
fXmlFile.open("w");
fXmlFile.write(xmlData);
fXmlFile.close();
} catch (e) {
alert("" + e.message + "\nThere are problems writing the XML file!");
}
return true;
} // --- End WriteXML ----------------------------
function GetXMLdata (sXmlFile) { // =======================================================
/* Read xml data 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);
Called by SwitchUIlanguage, GetSettingsList
Attention The language is not yet defined when this function is called by SwitchUIlanguage.
Therefor error messages need to be localised with the ExtendScript method
Reference E:\FM-ExtendScript\SaveBookOldVersion\SaveBookAsOldVersion.jsx by Rick Quatro
History 2020-11-11
*/
var fXmlFile, xData, msg_01, msg_02;
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) {
msg_01 = { en: "XML file «" + sXmlFile + "» not found.",
de: "XML datei «" + sXmlFile + "» nicht gefunden.",
fr: "Fichier XML «" + sXmlFile + "» non trouvé."};
KLD_F.Message ("E", localize (msg_01), "getXMLdata");
return false;
}
fXmlFile.open("r");
try {
xData = new XML(fXmlFile.read());
fXmlFile.close();
return xData;
} catch (e) { // what kind of error can this be ?
msg_02 = { en: "Read error on XML file «" + sXmlFile + "». Error:\n "+ e,
de: "Lesefehler auf XML datei «" + sXmlFile + "» . Fehler:\n "+ e,
fr: "Erreur de lecture sur le fichier XML «" + sXmlFile + "». Erreur:\n " + e};
KLD_F.Message ("E", localize (msg_02), "getXMLdata");
fXmlFile.close();
return false;
}
} //--- end getXMLdata