4 Replies Latest reply on Apr 28, 2016 9:55 AM by carlm80163462

    Deleting an XML object

    carlm80163462 Level 1

      Hi all,

       

      I've been having a bit of trouble deleting XML objects with ExtendScript's native delete() function. Basically I have an XML object being passed into a function like this:

       

      makeChanges(XMLObj, XMLObj.child1);
      makeChanges(XMLObj, XMLObj.child2);
      makeChanges(XMLObj, XMLObj.child3);
      
      function makeChanges(object, element)
      {
           if (condition)
                delete element;
      }
      

       

      However, the child element is not deleted, I suspect because the "element" variable reference is still active. If the code is changed to explicitly delete the object like so:

       

           if (condition)
                delete object.child1
      

       

      then everything works just fine, but I make quite a few calls to this function and would like the code to be re-usable if possible. I know JavaScript has a removeChild() function for XML, but does anyone know if there's an ExtendScript workaround? I've looked through the documentation and haven't seen much, so any input is greatly appreciated.

        • 1. Re: Deleting an XML object
          Klaus Göbel Level 3

          Hi carlm,

           

          "delete" is the right way.

           

          But maybe the access to the structure of xml is wrong.

           

          Just try my little snippet:

          It creates an XML-structure, adds two items and deletes an item-

           

          var gXML_Root = new XML ( "<FMProperties> <Item> </Item><Item></Item></FMProperties>");
          
           $.writeln(gXML_Root);
          
           // add some items
           gXML_Root.FMProperties.Item[0]= "Item 1";
           gXML_Root.FMProperties.Item[1]= "Item 2";
          
            $.writeln("-----------------");
            $.writeln(gXML_Root);
          
           // delete an item
           delete gXML_Root.FMProperties.Item[1];
             $.writeln(gXML_Root);
          
          • 2. Re: Deleting an XML object
            carlm80163462 Level 1

            Hi Klaus, thanks for replying. I'm afraid that your code is simply doing the same thing as the second example illustrated above (albeit using an index-based method, which I'm also not able to do as I can't guarantee the proper location of the child element).

             

            if (condition)
                 delete object.child1;     // same as delete object.child[indexOfChild];
            

             

            What I'm seeking to do is delete the XML child object after passing it into a function as it's own entity (the parent object is also passed in, if that helps).

             

            In your example, this would look something like this:

             

            var gXML_Root = new XML ("<FMProperties> <Item> </Item><Item></Item></FMProperties>");
            gXML_Root.FM_Properties.Item[0]= "Item 1";
            gXML_Root.FM_Properties.Item[1]= "Item 2";
            
            deleteChild(gXML_Root.FM_Properties, gXML_Root.FM_Properties.Item[1]);
            
            function deleteChild(parent, child)
            {
                 delete child;          // gXML_root.FM_Properties.Item[1] is not deleted
            }
            
            $.writeln(gXML_Root);
            

             

            Any ideas?

            • 3. Re: Deleting an XML object
              Klaus Göbel Level 3

              Hi carlm,

               

              another try:

              I hope, it comes closer to your needs.

               

              var gXML_Root = new XML ( "<FMProperties> <Item> </Item><Item></Item></FMProperties>");
              
               $.writeln(gXML_Root);
              
               // add some items
               gXML_Root.Item[0] = "Value 1";
               gXML_Root.Item[1]= "Item 2";
              
                $.writeln("-----------------");
                $.writeln(gXML_Root);
              
               // delete an item
               var DeleteIndex = 1;
               XML_Remove(gXML_Root,DeleteIndex)
              
                 $.writeln(gXML_Root);
                 
              function XML_Remove(fObject,fIndex)
              {
               var elem = fObject.elements();
               delete elem[fIndex];
              }
              
              • 4. Re: Deleting an XML object
                carlm80163462 Level 1

                I appreciate the help Klaus. Unfortunately I can't use the index as a selector because the location of the child element within the parent element varies, which is why I pass the child element to be deleted directly into the function.