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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:57:32+00:00 2026-05-24T05:57:32+00:00

I have a basic XML document setup like this: <rss> <channel> </channel> </rss> I

  • 0

I have a basic XML document setup like this:

 <rss>
      <channel>
      </channel>
 </rss>

I would like to add new child nodes of the channel as the first node, so, say I’m looping through a collection of items, then the first item would be added like this:

 <rss>
      <channel>
           <item>Item 1</item>
      </channel>
 </rss>

Then the next item would added like this:

 <rss>
      <channel>
           <item>Item 2</item>
           <item>Item 1</item>
      </channel>
 </rss>

I’ve been trying to use:

 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 Document doc = docBuilder.parse(new File(xmlFile));

 Element itemNode = doc.createElement("item");
 Node channelNode = doc.getElementsByTagName("channel").item(0);
 channelNode.appendChild(itemNode);

But it keeps adding new items to the bottom of the list.

  • 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-24T05:57:33+00:00Added an answer on May 24, 2026 at 5:57 am
    channelNode.appendChild(itemNode);
    

    will always append the itemNode to the end of the list of children of channelNode. This is behavior is defined in the DOM Level 3 specification, and in the JAXP documentation as well:

    Node appendChild(Node newChild)
    throws DOMException

        Adds the node newChild to the end of the list of children of this
    node (emphasis mine). If the newChild is already in the tree, it is first removed.

    If you need to add at the beginning of the list of child nodes, you can use the insertBefore() API method instead:

    channelNode.insertBefore(itemNode, channelNode.getFirstChild());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document with a basic structure like this: <?xml version=1.0 encoding=UTF-8
Greetings, I'm new to TCL scripting, and I have a very very basic xml
I have parsed a basic xml document with the following format <?xml version=1.0?> <data>
I need to add basic password protection to an Open XML Wordprocessing document. I
I have a basic XML object that I'm working with. I can't figure out
My knowledge of xml and php is somewhat basic but have been able to
In this case I have XML data source and external images files whole together
I have to generate winword document for badges printing application. Basic scenario is the
I have a long document in XML from which I need to produce static
I have an existing xml document with music playlist information, which is read into

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.