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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:31:20+00:00 2026-06-12T10:31:20+00:00

I want to add an XPath attribute to all <definition> tags in my XML

  • 0

I want to add an XPath attribute to all <definition> tags in my XML document. The XPath’s value will come from a nested tag <term>Test123</term> inside each <definition> tag. I’m using the import org.w3c.dom.Document; object. When I run this line with my really long XML file then it tells me that there are 539 instances of the <definition> tag:

System.out.println(inputDOM.getDocumentElement().getElementsByTagName("definition"));

So the end result is that there must be a Document object that has set all the definition tags from the term tags, for example

<definition XPath="Test123"> <term>Test123</term> </definition>

  • 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-12T10:31:21+00:00Added an answer on June 12, 2026 at 10:31 am

    Try something like this

    NodeList definitionElements = inputDOM.getElementsByTagName("definition");
    for (int i = 0; i < definitionElements.getLength(); i++) {
        Element current = (Element) definitionElements.item(i);
        Node term = current.getElementsByTagName("term").item(0);
        if(term != null && term.getNodeType() == Node.ELEMENT_NODE)
            current.setAttribute("XPath", ((Element) term).getTextContent());
    }
    

    First we get all <definition> tags. After that we iterate each <definition> tag and set a new attributs “XPath” whose value is the text content of the <term> child tag

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

Sidebar

Related Questions

I want add my custom sidebar next right column all page. Please check this
I have problem, when I want add Node to my GUI from other Thread.
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I want to add an XML fragment to the last element to an XML
I have an xml document like so: <add-server-response> <server id=4029 name= notes= state=Restoring Harddrive
I want to take an attribute found thru xpath and replace it in the
I want to add an XML schema(Main.xsd) to a dataset which includes another schema(base.xsd)
I have an xml document as a string without any namespace and I want
I have next XML: <?xml version=1.0 encoding=UTF-8?> <mailAndMessageSettings> <settings> <add key=Url value=/> <add key=UserName
Given an XML fragment that I want to parse with XPath I first need

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.