Skip to main content
Inspiring
February 1, 2016
Answered

XML declaration

  • February 1, 2016
  • 1 reply
  • 771 views

When writing an XML object to file is it possible to include the XML declaration?


I've tried the toXMLString() function and that doesn't seem to work, I get the XML file contents just fine but no declaration.


I've tried adding it as a string literal like so (the book object is simple an XML object containing all of the book contents):

var xmlFile = File(chapterFolder+app.activeDocument.name.replace(".indd","")+".xml");

xmlFile.open('w');

xmlFile.encoding = 'UTF-8';

xmlFile.write("""<?xml version="1.0" encoding="UTF-8"?>"""+"\r"+book.toXMLString());

xmlFile.close();

Is there a property I'm missing that would allow me to have the declaration included automatically?

Cheers,

Kurtis

This topic has been closed for replies.
Correct answer frameexpert

Hi Kurtis,

Is it an ExtendScript XML object? I am able to do this in one of my scripts and get the declaration:

var settingsXML = <settings><test-element>Element text</test-element></settings>;

saveSettingsFile (settingsXML);

function saveSettingsFile (settingsXML) {

    var settingsFile = File ($.fileName.replace (/\.jsx(?:bin)?$/i , ".xml"));

    settingsFile.open ("w");

    settingsFile.write ("<?xml version='1.0'?>\r" + settingsXML);

    settingsFile.close ();

}

This script give me this result:

<?xml version='1.0'?>

<settings>

  <test-element>Element text</test-element>

</settings>

-Rick

1 reply

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
February 1, 2016

Hi Kurtis,

Is it an ExtendScript XML object? I am able to do this in one of my scripts and get the declaration:

var settingsXML = <settings><test-element>Element text</test-element></settings>;

saveSettingsFile (settingsXML);

function saveSettingsFile (settingsXML) {

    var settingsFile = File ($.fileName.replace (/\.jsx(?:bin)?$/i , ".xml"));

    settingsFile.open ("w");

    settingsFile.write ("<?xml version='1.0'?>\r" + settingsXML);

    settingsFile.close ();

}

This script give me this result:

<?xml version='1.0'?>

<settings>

  <test-element>Element text</test-element>

</settings>

-Rick

www.frameexpert.com
KuddRowwAuthor
Inspiring
February 1, 2016

Thanks for testing! That works. I didn't realize that I'm outputting two XML files, one with a file name including date and one without. I was testing the code against the one and checking the other. Thanks for taking the time to test and respond to this.

frameexpert
Community Expert
Community Expert
February 1, 2016

My pleasure. Thanks for marking it Correct!

www.frameexpert.com