• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Self closing xml elements considered harmful

Community Expert ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

Dear friends, experts and gurus!

Main purpose of my project FMfindRepl is saving the settings to an xml file and get them back later.

It turns out that 'roung-tripping' empty edit-fields is not handled correctly. It even leads to blocked functions (Buttons will not work any more).

  • Field "find what" is empty for various find operations, for example when searching for Anchored Frames. This is represented in the xml file as

 

 <findstring></findstring>​

 

  • At the next start of FM with the script  things work as expected.
  • As long as I do not save a new set of settings nothing strange happens and the next FM session is OK.
  • Saving new settings of course recreates the xml file. And now the previously existing 

 

<findstring></findstring>​

 

are saved to the file as

 

 <findstring/>​

 

  • This is absolutely correct in the sense of XM, but ...
  • ... in the next FM session these empty elements create problems. There is no more a distinction between an empty string, a null or undefined element. See my  test script (after dl rename *.txt to *.jsx).

A crunch would be to postprocess the written xml file and replace all self-terminated elements by ordinary elements with start and end tags (see stackexchange).

<(\w+)/>       →     <$1></$1>

But I want to avoid writing these self-terminated elements directly.

→ How could this be achieved?

TOPICS
Scripting

Views

193

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 22, 2021 Mar 22, 2021

For the time being I decided to modifiy the written xml file with a function:

KLD_F.ConvertSelfTerminatedElements = function (xFile) { // ==========================
/*            Expand self-terminating xml elements to empty start-tag end-tag elements
Arguments     xFile           xml file to be modified
Returns       false in case of error, else true
Called by     ButtonDelete, ButtonSave, ButtonSaveNew
Comment       Self-terminating xml elements create problems when they are read. 
            
...

Votes

Translate

Translate
Community Expert ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

LATEST

For the time being I decided to modifiy the written xml file with a function:

KLD_F.ConvertSelfTerminatedElements = function (xFile) { // ==========================
/*            Expand self-terminating xml elements to empty start-tag end-tag elements
Arguments     xFile           xml file to be modified
Returns       false in case of error, else true
Called by     ButtonDelete, ButtonSave, ButtonSaveNew
Comment       Self-terminating xml elements create problems when they are read. 
              Buttons Retrieve, Save, SaveNew, Delete and Reset do not work any more.
Reference     https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Used in       FMfindRepl
History       2021-03-22
*/
var BOM = "\uFEFF", eReg1, eReg2, fXFile, xData;

function replacer (match, p1) {
  return "<" + p1 + "></" + p1 + ">";
}

  if ((xFile == null) || (xFile == undefined)) {
    return false;
  } else if (xFile.indexOf("/") > 0 || xFile.indexOf("\\") > 0) { // fully qualified path
    fXFile = new File(xFile);
  } else {
    fXFile = new File($.fileName.replace (/[^\\\/]+$/i , xFile));  // file in script folder
  }
  if (fXFile.exists === false) {
    KLD_F.Message ("E", KLD_F.UItxt.ConvertSelfTerminatedElements.@msg_01, "ConvertSelfTerminatedElements", sXmlFile, e.message);
      return false;
  }
  fXFile.open("r");
  xData = fXFile.read();
  fXFile.close ();
  xData = xData.replace (/<(\w+)\/>/g , replacer);
  fXFile.open('w');
  fXFile.encoding = "UTF-8";
  fXFile.write(BOM);
  fXFile.write(xData);
  fXFile.close();

  return true;
} //--- end ExpandSelfTerminatedElements -------------------------------------

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines