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

[JS] Write XML file

Enthusiast ,
Oct 28, 2013 Oct 28, 2013
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

Capture d’écran 2013-10-28 à 18.56.32.png

but I opt cesi

Capture d’écran 2013-10-28 à 18.56.47.png


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

TOPICS
Scripting
2.2K
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
Enthusiast ,
Oct 28, 2013 Oct 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

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
Enthusiast ,
Oct 28, 2013 Oct 28, 2013
Thanks, but do you have an example because I had already test but I can't get it to work.

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
Enthusiast ,
Oct 28, 2013 Oct 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:

Screen Shot 2013-10-28 at 2.59.16 PM.PNG

XML.prettyPrinting = true;

alert(xml.toXMLString());

looks right:

Screen Shot 2013-10-28 at 2.59.36 PM.PNG

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
Enthusiast ,
Oct 28, 2013 Oct 28, 2013

 

OK, I have not too usual but the configuration of these deuxligne and this function:


XML.prettyIndent = 5;

XML.prettyPrinting = true;

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
Enthusiast ,
Oct 28, 2013 Oct 28, 2013
I would like to used the class "namespace".


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

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
Enthusiast ,
Oct 29, 2013 Oct 29, 2013
LATEST

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

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