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 9134579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:41:15+00:00 2026-06-17T08:41:15+00:00

Let’s consider the following XML document: <items> <item>item1</item> <item>item2</item> </items> Now, let’s remove all

  • 0

Let’s consider the following XML document:

<items>
   <item>item1</item>
   <item>item2</item>
</items>

Now, let’s remove all the items and add some new item. Code:

  //-- assume we have Element instance of <items> element: items_parent
  //   and the Document instance: doc

  //-- remove all the items
  NodeList items = items_parent.getElementsByTagName("item");

  for (int i = 0; i < items.getLength(); i++){
     Element curElement = (Element)items.item(i);
     items_parent.removeChild(curElement);
  }

  //-- add a new one
  Element new_item = doc.createElement("item");
  new_item.setTextContent("item3");
  items_parent.appendChild(new_item);

New contents of the file:

<items>


   <item>item3</item>
</items>

These annoying blank lines appeared because removeChild() removes child, but it leaves indent of removed child, and line break too. And this indent_and_like_break is treated as a text content, which is left in the document.

In the related question I posted workaround:

items_parent.setTextContent("");

It removes these blank lines. But this is some kink of hack, it removes the effect, not the cause.

So, the question is about removing the cause: how to remove child with its intent with line break?

  • 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-06-17T08:41:16+00:00Added an answer on June 17, 2026 at 8:41 am

    The “indent” before the element and the “carriage return” (and following indent) after it are text nodes. If you remove an element and there’s a text node before or after it, naturally those nodes are not removed.

    It sounds as though you want to remove the element, and then also remove the text node in front of it (provided it consists entirely of whitespace).

    E.g., perhaps along these lines (in your loop removing items):

     Element curElement = (Element)items.item(i);
     // Start new code
     Node prev = curElement.getPreviousSibling();
     if (prev != null && 
         prev.getNodeType() == Node.TEXT_NODE &&
         prev.getNodeValue().trim().length() == 0) {
         items_parent.removeChild(prev);
     }
     // End new code
     items_parent.removeChild(curElement);
    

    However, the real question should probably be why your XML document has extraneous whitespace text nodes in it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Let's consider an app doing the following steps using AppleScript, in order to auto-login
Let's assume I have the following typedef: typedef void (^myBlock)(id); And I have some
Let's say I have some text as follows: do this, do that, then this,
let's say I have the following string: string s = A B C D
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I wanted to remove vowels from HTML: <a href=foo>Hello there!</a>Hi! becomes <a
let's say.. I have the following java bean. Case1: (Student Bean) Integer id; String
Let's say if the user is able to select some directories to search. And
Let's say I got this string: $str = alemylaife; (I know it's misspelled, all

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.