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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:48:08+00:00 2026-05-28T16:48:08+00:00

Say I have a dom node like so: org.w3c.dom.Element node: <node id=101> <node id=102>

  • 0

Say I have a dom node like so:

org.w3c.dom.Element node:

<node id="101">
  <node id="102">
    <node id="103" />
  </node>
  <node id="104">
    <node id="103" />
  </node>
</node>

To transform the id attribute I would do the following in my Java Code:

String nodeId = node.getAttribute("id");
String newNodeId = "prefix/" + nodeId;
node.getAttributeNode("id").setValue(newNodeId);

The above node would then is then transformed to:

<node id="prefix/101">
  <node id="102">
    <node id="103" />
  </node>
  <node id="104">
    <node id="103" />
  </node>
</node>

But, I want to recursively modify all the subnodes. The expected transformed node is:

<node id="prefix/101">
  <node id="prefix/102">
    <node id="prefix'103" />
  </node>
  <node id="prefix/104">
    <node id="prefix/103" />
  </node>
</node>

I could loop through the child nodes but then node may have multiple levels of children. In this case, the root has two sub-levels. But, if there are more sub-levels then looping through child nodes of every level seems a bit awkward. Is there a more straight forward approach to achieve this task?

Thanks,
Sony

  • 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-28T16:48:09+00:00Added an answer on May 28, 2026 at 4:48 pm

    You could use XPath to get a list of all the nodes you want to modify:

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    
    ...
    
    final String xml = "<node id=\"101\"><node id=\"102\"><node id=\"103\" /></node><node id=\"104\"><node id=\"103\" /></node></node>";
    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    final DocumentBuilder db = dbf.newDocumentBuilder();
    final Document doc = db.parse(new InputSource(new StringReader(xml)));
    final XPathFactory xpathFactory = XPathFactory.newInstance();
    final XPath xpath = xpathFactory.newXPath();
    final NodeList nodes = (NodeList) xpath.compile("//node[@id]").evaluate(doc, XPathConstants.NODESET);
    for (int nodeNumber = 0; nodeNumber < nodes.getLength(); ++nodeNumber) {
        final Element node = (Element) nodes.item(nodeNumber);
        final String nodeId = node.getAttribute("id");
        final String newNodeId = "prefix/" + nodeId;
        node.getAttributeNode("id").setValue(newNodeId);
    }
    

    The document will now consist of this:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <node id="prefix/101">
        <node id="prefix/102">
            <node id="prefix/103" />
        </node>
        <node id="prefix/104">
            <node id="prefix/103" />
        </node>
    </node>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have an org.w3c.dom.Document representing the following XML: <Fruits> <Apples> <Red/> <Green/>
Say I have an element like this... <math xmlns=http://www.w3.org/1998/Math/MathML> <mo class=symbol>α</mo> </math> Is there
Say I have an XML document (represented as text, a W3C DOM, whatever), and
Let say I have the following DOM element on my page: <div id=name>Sharon</div> Q1:
Say I have a source document like this: <element> <subelement xmlns:someprefix=mynamespace/> </element> The xmlns:someprefix
Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Say I have a class named Frog, it looks like: public class Frog {
I'm new into PHO DOM Parser. I have a string like this : $coded_string
Say you in JavaScript are handed a single DOM element or Document (for example
Say I have a Google maps v2 GMarker like this: var marker = new

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.