Skip to main content
Liphou
Inspiring
October 28, 2013
Question

[JS] Write XML file

  • October 28, 2013
  • 1 reply
  • 2446 views
Hello


I read an XML file I y contribution of the change and I want to write it in a new XML file.


I would get this presentation

but I opt cesi


Here is my code :

    leDossierOEBPS = "~/Desktop/content/";

    monEcritureContent(File (leDossierOEBPS + '/content.opf'),File (leDossierOEBPS + '/content5.opf'));

function monEcritureContent(refFile,refFile2) {

   default xml namespace = 'http://www.idpf.org/2007/opf';

 

    var XMLHeader = '<\?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>';

    var XMLWorkbookHeader = "<\?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";

    var XMLWorkbookHeader2 = " xmlns=\"http\://www.idpf.org/2007/opf\" unique-identifier=\"bookid\" version=\"2.0\"";

  

           if(refFile != null) {

                        refFile.open("r");

                        var XMLToParse = refFile.read();

                        refFile.close();

                    

                        XMLToParse = XMLToParse.replace(XMLWorkbookHeader,"");

                        XMLToParse = XMLToParse.replace(XMLWorkbookHeader2,"");

                       

                   

                        var XMLData2 = new XML(XMLToParse);

                        XMLToParse = null;

            } else {

                    alert("Error opening XML file.");

            }

 

refFile2.open("w");

refFile2.encoding = "UTF8";

refFile2.writeln (XMLHeader+XMLData2.toXMLString());

refFile2.close();

}//


What is the correct control to write the XML tree?

Thank you

This topic has been closed for replies.

1 reply

Inspiring
October 28, 2013

The XML object has some class properties ("prettyPrinting", "prettyIndent") that are probably relevant: http://jongware.mit.edu/idcs6js/pc_XML.html . But I think the defaults should give you what you want, so I don't know why you're seeing what you're seeing if you haven't changed them.

Jeff

Liphou
LiphouAuthor
Inspiring
October 28, 2013
Thanks, but do you have an example because I had already test but I can't get it to work.

Inspiring
October 28, 2013

Are you setting those properties on the class or an instance? It's a class property.

var file = new File("~/Desktop/temp/sample.xml"),

    xml;

file.open('r');

xml = new XML(file.read());

file.close();

XML.prettyPrinting = false;

alert(xml.toXMLString());

gives you the smushed-together output:

XML.prettyPrinting = true;

alert(xml.toXMLString());

looks right: