1 Reply Latest reply on Jul 8, 2014 9:46 AM by mrObvious

    How to replace a text element of XML structure with an image.

    mrObvious

      I need to replace specific elements within my XML structure with an image and I'm having problems using the existing XML structure for this.

      I was hoping for something as simple as myDoc.xmlElements[0].xmlElements[0].xmlElements[0].contents = new File("MyImgPath.ext");

       

      I can use GREP to find the text of each item in my XML structure I wish to replace, remove the text and place the image and the result is what I want.  However, in the rare situation where the same text as the header, is in one of the paragraphs it too would be replaced with the image and we can't have that.

       

      My XML structure is slightly more complicated but this example has the necessary elements:

      > Root

           > Group

                > Header

                > Item

        • 1. Re: How to replace a text element of XML structure with an image.
          mrObvious Level 1

          Well I managed to find a solution to my problem.  I'm not sure if it's the best way to go about it but it works for me.

           

          var myDoc = app.open('//the/path/to/my/doc', OpenOptions.OPEN_COPY);

          var myXML = myDoc.xmlElements[0].xmlElements[x].xmlElements[0];

          var myImg = new FIle('//path/to/my/image');

           

          myXML.contents = ''; // To clear out all the text

          myDoc.select( myXML ); // Selects the xmlElement

          myDoc.selection[0].insertionPoints[0].place( myImg ); // Places the image in the selection