[JS] Write XML file
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
XML.prettyIndent = 5;
XML.prettyPrinting = true;
Copy link to clipboard
Copied
Currently I use it like this:
default xml namespace = 'http://www.idpf.org/2007/opf';
but I also have to put the two following
unique - identifier = "bookid" version = "2.0".
But my its too short deep.
Thank you
Copy link to clipboard
Copied
Thank you I went out with this
leFichierContentXML.@['xmlns'] = 'http://www.idpf.org/2007/opf';
leFichierContentXML.@['unique-identifier'] = 'bookid';
leFichierContentXML.@['version'] = '2.0';
Thank you

