Skip to main content
Inspiring
June 15, 2009
Answered

CS3 Win/Mac: Create an XML file and add content

  • June 15, 2009
  • 6 replies
  • 2534 views

Hi

I want create an XML file on a specified location. This file contains some information about images in an Indesign document.

- Can I create a new XML file in Javascript?

- If yes, how can I add some content?

Thanks for all hints

Hans

This topic has been closed for replies.
Correct answer interesting_Flower157F

I made a small example, just to know how it works.

I dont know if there is a smarter way of using the XMLobject, i guess that is what harbs's link is about, havent read that yet though.

var myRootXmlObj = new XML ("<root><fonts></fonts></root>");
var font = "";
var myInstalledFonts = app.fonts.everyItem().name;

for(i = 0; i < myInstalledFonts.length; i++) {
    font = new XML ("<font>" + myInstalledFonts + "</font>");
    myRootXmlObj.fonts.appendChild(font);
}

var f = new File("myTestFile.txt"); // on windows XP this goes in the user documents folder

writeXMLFile(f, myRootXmlObj);
alert(readXMLFile(f));

// rom forum thread http://forums.adobe.com/message/1114070#1114070
function writeXMLFile(file, xml) {
    if (!(xml instanceof XML)) {
        throw "Bad XML parameter";
    }
    file.encoding = "UTF8";
    file.open("w", "TEXT", "????");
    // unicode signature, this is UTF16 but will convert to UTF8 "EF BB BF"
    file.write("\uFEFF");
    file.lineFeed = "unix";
    file.write(xml.toXMLString());
    file.close();
};

function readXMLFile(file) {
    if (!file.exists) {
        throw "Cannot find file: " + decodeURI(file.absoluteURI);
    }
    file.encoding = "UTF8";
    file.lineFeed = "unix";
    file.open("r", "TEXT", "????");
    var str = file.read();
    file.close();
    return new XML(str);
};

6 replies

interesting_Flower157F
Inspiring
June 16, 2009

I made a small example, just to know how it works.

I dont know if there is a smarter way of using the XMLobject, i guess that is what harbs's link is about, havent read that yet though.

var myRootXmlObj = new XML ("<root><fonts></fonts></root>");
var font = "";
var myInstalledFonts = app.fonts.everyItem().name;

for(i = 0; i < myInstalledFonts.length; i++) {
    font = new XML ("<font>" + myInstalledFonts + "</font>");
    myRootXmlObj.fonts.appendChild(font);
}

var f = new File("myTestFile.txt"); // on windows XP this goes in the user documents folder

writeXMLFile(f, myRootXmlObj);
alert(readXMLFile(f));

// rom forum thread http://forums.adobe.com/message/1114070#1114070
function writeXMLFile(file, xml) {
    if (!(xml instanceof XML)) {
        throw "Bad XML parameter";
    }
    file.encoding = "UTF8";
    file.open("w", "TEXT", "????");
    // unicode signature, this is UTF16 but will convert to UTF8 "EF BB BF"
    file.write("\uFEFF");
    file.lineFeed = "unix";
    file.write(xml.toXMLString());
    file.close();
};

function readXMLFile(file) {
    if (!file.exists) {
        throw "Cannot find file: " + decodeURI(file.absoluteURI);
    }
    file.encoding = "UTF8";
    file.lineFeed = "unix";
    file.open("r", "TEXT", "????");
    var str = file.read();
    file.close();
    return new XML(str);
};

hstoesselAuthor
Inspiring
June 30, 2009

Hi

Thanks a lot for the help.

Hans

interesting_Flower157F
Inspiring
June 16, 2009

Have a look at chapter 9 in "Integrating XML into JavaScript" from the Javascript Tools Guide (PDF file - can be found in ESTK 2 under Help -> SDK -> ...)

You should be able to use that to create an XML object and then write that to a file.

Harbs.
Legend
June 16, 2009

You should be able to use that to create an XML object and then

write that to a file.

Yes. Hence my reference to E4X...

Do yourself a favor and read up on E4X!

Harbs

Andreas Jansson
Inspiring
June 15, 2009

Your question indicates that you would like to add other data to the XML, than the data actually in the document (did I get that right?) - and perhaps you have not the possibility to tag the document. Anyway, depending on what you are looking for, the built-in XML export can export XML tagged objects to file, just as using the Export menu item.

Exporting a tagged story object to an XML file:

theStory.associatedXMLElement.exportFile(ExportFormat.xml, File(myFilePath));

hstoesselAuthor
Inspiring
June 16, 2009

Hi

Thanks for all hints.

I want to write a xml file with a self defined content, not depending on an InDesign document. I want to analyse a open document and write a xml file with the information about it in a self definied format.

Thanks

Hans

Harbs.
Legend
June 15, 2009

https://developer.mozilla.org/en/e4x

Harbs

Andreas Jansson
Inspiring
June 15, 2009

Hi!

Have a look at the File object. It has functions for writing to file. The help file had a chapter called "Using File and Folder objects", I think.

Use File.open to open a file, for reading, writing or editing (an exising file).

Here is a small sample function:

function writeToLog(msg){

    // Errors trying to open the file for edit must not result in a finally case trying to close the file.
    var f = new File(myPath);

    f.open('e');  // For edit

    try{
        f.seek(0, 2); // Seek backwards from end (2), to the last (0) position.
        f.writeln (msg);
    }catch(e){
        throw('Errors writing to log file: "' + myPath + '"' + '\n' + e.toString());
    }finally{
        f.close();

    }

}

Harbs.
Legend
June 15, 2009

Email access is SO unreliable...

https://developer.mozilla.org/en/e4x

Harbs