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

Remove xml from a file

New Here ,
May 28, 2019 May 28, 2019

Hi Guys,

I have problem to remove an xml from my indesign file.

If I use this syntax:

1. doc = app.activeDocument;

2. var rootXE = doc.xmlElements.item(0).xmlElements.item(0);

3. rootXE.remove();

the xml file is correctly removed.

But if I delete the third line and I use a button in a dialog window like:

loadXml_button.onClick = function () {

rootXE.remove();

}

the script fail.

if  I set an alert in:

loadXml_button.onClick = function () {

alert(rootXE)

rootXE.remove();

}

He return right xml object.

Anyone can help me?

Thanks.

TOPICS
Scripting
1.0K
Translate
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 , May 28, 2019 May 28, 2019

Place the following line at the top of the code

#targetengine "test"

and change "dialog" to "palette" in your Window constructor, line no 7 and then try

-Manan

Translate
Community Expert ,
May 28, 2019 May 28, 2019

hi Daniel,

Cn you post the whole code of the dialog as well and also share which version of Indesign do you see the issue with

-Manan

Translate
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
New Here ,
May 28, 2019 May 28, 2019

INDESIGN CC18

this is my code

////// Variables

doc = app.activeDocument;

var rootXE = doc.xmlElements.item(0).xmlElements.item(0);

////// Dialog window

var w = new Window ("dialog","Price List Creator");

var xmlGroup = w.add ("group");

var xmlGroupSub = w.add ("group");

w.alignChildren = 'left';

xmlGroup.add ("statictext", undefined, 'XML Action\r-------\rLoaded xml delete old loaded xml', {multiline: true});

var loadXml_button =  xmlGroupSub.add ("button", undefined, "Load XML");

loadXml_button.onClick = function () {

var blnFile = verifyXML();

if(blnFile == false){

alert("Unable to process XML file.");

}

else if(blnFile == true){

rootXE.remove();

  }

}

var generateGroup = w.add ("group");

generateGroup.add ("statictext", undefined, 'Duplicate Group');

var generateGroupSub = w.add ("group");

var generate_button =  generateGroupSub.add ("button", undefined, "Generate");

generate_button.onClick = function () {

docSetup();

}

w.add ("button", undefined, "OK");

w.show ();

Translate
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
Community Expert ,
May 28, 2019 May 28, 2019

The code seems to be incomplete, i suppose the code having issues is within the loadXML_button click handler but it contains a method call verifyXML whose definition is not present in the code. If i comment out this code then also i get an error that document operation can't be done when modal dialog is open, on changing the type from dialog to palette it does delete a node from the XML

-Manan

Translate
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
New Here ,
May 28, 2019 May 28, 2019

Yeah I paste only necessary code, loadXML_button don't have any problem.

My only problem is delete rootXE, because if I put "remove" inside of any function:

rootXE.remove();

this not work.

complete code:

////// Variables

doc = app.activeDocument;

var rootXE = doc.xmlElements.item(0).xmlElements.item(0);

////// Dialog window

var w = new Window ("dialog","Price List Creator");

var xmlGroup = w.add ("group");

var xmlGroupSub = w.add ("group");

w.alignChildren = 'left';

xmlGroup.add ("statictext", undefined, 'XML Action\r-------\rLoaded xml delete old loaded xml', {multiline: true});

var loadXml_button =  xmlGroupSub.add ("button", undefined, "Load XML");

loadXml_button.onClick = function () {

var blnFile = verifyXML();

if(blnFile == false){

alert("Unable to process XML file.");

}

else if(blnFile == true){

rootXE.remove();

  }

}

var generateGroup = w.add ("group");

generateGroup.add ("statictext", undefined, 'Duplicate Group');

var generateGroupSub = w.add ("group");

var generate_button =  generateGroupSub.add ("button", undefined, "Generate");

generate_button.onClick = function () {

docSetup();

}

w.add ("button", undefined, "OK");

w.show ();

//////////

function verifyXML(){

    xmlFile = File.openDialog("Please locate the XML data.");

    if (xmlFile == null){

            return false;

    }else{

        try{

             xmlFile.open("r");

             var xmlImport = new XML(xmlFile.read());

             if (xmlImport){

                return true;

            }else{

                return false;

            }

        }

        catch(err){

        return false;

        }

    }

}

//////////////////

function docSetup(){

alert("do");

}

Translate
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
Community Expert ,
May 28, 2019 May 28, 2019

Place the following line at the top of the code

#targetengine "test"

and change "dialog" to "palette" in your Window constructor, line no 7 and then try

-Manan

Translate
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
New Here ,
May 28, 2019 May 28, 2019

Great! now work like a charm!!!!

Why?! where is the problem of my code?

#targetengine  how work this?

Translate
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
Community Expert ,
May 28, 2019 May 28, 2019
LATEST

You were creating a modal dialog which stops all execution/interaction until it is dismissed, hence it was not able to make changes to the document. When you use the window type as palette it does not stop you from interacting with the InDesign UI so your code is able to make the changes in the document

As regards targetengine, it is used to create a persistent space for your script to execute, so that things from your script are alive even when your code exits, ex your event handler. If you remove this the UI will appear and then disappear simultaneously.

-Manan

Translate
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