Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7650401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:14:37+00:00 2026-05-31T11:14:37+00:00

With the following XML: <parent> <child>Stuff</child> <child>Stuff</child> </parent> Using XPath I query the child

  • 0

With the following XML:

<parent>
    <child>Stuff</child>
    <child>Stuff</child>
</parent>

Using XPath I query the child elements, and based on some conditions I want to append an extra parent level above some of them:

<parent>
    <extraParent>
        <child>Stuff</child>
    </extraParent>
    <child>Stuff</child>
</parent>

What’s the best way to do this?

I was thinking something along the following lines:

Nodes childNodes = parent.query("child");
for (int i = 0; i < childNodes.size(); i++) {
    Element currentChild = (Element) childNodes.get(i);
    if (someCondition) {
        ParentNode parent = currentChild.getParent();
        currentChild.detach();
        Element extraParent = new Element("extraParent");
        extraParent.appendChild(currentChild);
        parent.appendChild(extraParent);
    }
}

But I want to preserve the order. Possibly this could be done using parent.insertChild(child, position)?

Edit: I think the following works, but I’m curious if anyone has a better way:

Elements childElements = parent.getChildElements();
for (int i = 0; i < childElements.size(); i++) {
    Element currentChild = childElements.get(i);
    if (someCondition) {
        ParentNode parent = currentChild.getParent();
        currentChild.detach();
        Element extraParent = new Element("extraParent");
        extraParent.appendChild(currentChild);
        parent.insertChild(extraParent,i);
    }
}

Edit 2: This is possibly better, as it allows you to have other elements mixed in with the child elements that you aren’t interested in:

Nodes childNodes = parent.query("child");
for (int i = 0; i < childNodes.size(); i++) {
    Element currentChild = (Element) childNodes.get(i);
    if (someCondition) {
        ParentNode parent = currentChild.getParent();
        int currentIndex = parent.indexOf(currentChild);
        currentChild.detach();
        Element extraParent = new Element("extraParent");
        extraParent.appendChild(currentChild);
        parent.insertChild(extraParent,currentIndex);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:14:38+00:00Added an answer on May 31, 2026 at 11:14 am

    This seems to work adequately:

    Nodes childNodes = parent.query("child");
    for (int i = 0; i < childNodes.size(); i++) {
        Element currentChild = (Element) childNodes.get(i);
        if (someCondition) {
            ParentNode parent = currentChild.getParent();
            int currentIndex = parent.indexOf(currentChild);
            currentChild.detach();
            Element extraParent = new Element("extraParent");
            extraParent.appendChild(currentChild);
            parent.insertChild(extraParent,currentIndex);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want the following query to return the fields in a parent child format.
There is my xml: <parent> <children> <child>1</child> <child>2</child> </children> </parent> I want to have
I'm trying to parse some Android XML using XmlSlurper . For a given child
I have the following xml input: <data> <parent Id=1 value=ParentOne> <child x=1 y=2 />
I have an XML file similar in structure to the following: <Parent> <Child>5</Child> <Child>3</Child>
I have the following dir structure: parent-proj - child proj1 - pom.xml -child proj2
I am wondering how to obtain the following from a query using xml on
Given the following XML: <cfsavecontent variable=xml> <root> <parent> <child>I'm the first</child> <child>Second</child> <child>3rd</child> </parent>
I want to inherit the dependencies of a (parent) pom.xml in a child project
I am now using xpath to test a node's parent node's immediate following sibling(uncle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.