Skip to main content
Participant
October 24, 2013
Question

Moving an xmlElement to make it the child of another

  • October 24, 2013
  • 1 reply
  • 723 views

Hi all,

I'm looking for a solution to move one xmlElement so that it becomes the child of another.  For example, the current structure is:

<Root>

     <Story>

     <Article>

And I'd like to move the Story element to make the structure:

<Root>

     <Article>

          <Story>

I've tried various methods:

  • Story.move(LocationOptions.AT_BEGINNING, Article)
  • Article.markup(Story)
  • Story.markupTag = Article)
  • app.activeDocument.xmlElements[0].xmlElements.add("Article", Story)

In these examples, "Story" and "Article" are variables that contain the appropriate XML element.

None of those attempts has worked.  Is there a scripting solution to make one XML element the child of another?

Thanks!

This topic has been closed for replies.

1 reply

Legend
October 24, 2013

Try this,

var root = app.activeDocument.xmlElements[0];

root.xmlElements.item("Story").move(LocationOptions.AT_END, root.xmlElements.item("Article"));

Vandy

jbraxxAuthor
Participant
October 24, 2013

Thanks, but that just seems to move the Story element after the Article element, but doesn't place Story inside the Article element.  Any other ideas?

Legend
October 25, 2013

No problem at my end, it is working fine for single iteration.

If you want to iterate loop for the entire story,

Try the below code,

//-----------------------

findStoryTag(app.activeDocument);

function findStoryTag(elem)

{

    for (var i = 0; i < elem.xmlElements.length; i++)

    {

        XMLelementName=elem.xmlElements.markupTag.name.toString();

        if(XMLelementName=="Story")

        {

            elem.xmlElements.move(LocationOptions.AT_END, elem.xmlElements[i-1]);

            continue;

        }

        findStoryTag(elem.xmlElements);

    }

}

Vandy