1 Reply Latest reply on Sep 15, 2012 2:22 AM by Kasyan Servetsky

    Getting xmlcontent string from xmlElement object

    TD_TAD Level 1

      Hello All,


      Suppose paragraph in text frame contains xml structure as,




      Hello friends



      When I get contents of xmlElement P it gives only strings "Hello Hello friends", but I want it as,  "<p><bold><italic>Hello</italic></bold>Hello friends</p>".


      Is there any simple way to get this? Without exporting P tag as xml and reading that xml back


      Please help me out.




        • 1. Re: Getting xmlcontent string from xmlElement object
          Kasyan Servetsky Level 5

          I would handle this task like so:


          function Main() {
                          var xmlElement
                          var doc = app.activeDocument;
                          var xmlRoot = doc.xmlElements[0];
                          var xmlElements = xmlRoot.evaluateXPathExpression("//p");
                          for (var i = 0; i < xmlElements.length; i++) {
                                          xmlElement = xmlElements[i];
                                          var xmlContent = xmlElement.xmlContent;
                                          var xmlFile = new File("~/Desktop/Temp.xml");
                                          xmlElement.exportFile(ExportFormat.XML, xmlFile, false);
                                          var str = ProcessFile(xmlFile);
                                          $.writeln(i + " - " + str);
          function ProcessFile(file) {
                          var txt = file.read();
                          txt = txt.replace('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n', '');
                          return txt;

          Export every xml-element to a temporary xml-file, read it and remove the XML declaration line.


          Here is how my test file looks like:

          15-09-2012 12-11-56.png

          This is the result I get in console:

          15-09-2012 12-15-36.png

          Hope this helps.