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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:13:24+00:00 2026-06-05T20:13:24+00:00

I am using jxpath to print all nodes and add a child node to

  • 0

I am using jxpath to print all nodes and add a child node to the feature tag in this xml

<extracts>
<extract>
<id>1</id>
<features>
<feature>1</feature>
<feature>2</feature>
</extract>
</extracts>

This is what my code looks like (the part that works at least – it prints some information):

    import org.apache.commons.jxpath.ri.model.*;
    import org.apache.commons.jxpath.JXPathContext;
    import org.apache.commons.jxpath.Pointer;


try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(getBytesFromFile(file));
Document doc = builder.parse(bais);

JXPathContext jxpathCtx = JXPathContext.newContext(doc.getDocumentElement());
jxpathCtx.setLenient(true);

The first part of my requirement – which is to print these nodes — is trivial:

for (Iterator iter2 = jxpathCtx.iterate("/extract/*"); iter2.hasNext();) 
{
    System.out.println("\n Value is : " + iter2.next().toString() +"\n");

}

The second part of my requirement is what gets to me

I need to add a new entry –a new < feature >3< /feature > node UNDER the existing <features> tag under < extract > programatically

It could be something along the lines of isolating that node – and then adding a child to it – i just dont know how to go about it :

org.apache.commons.configuration.HierarchicalConfiguration.NodeNode node = (Node)jxpathCtx.selectNodes("/extract/lastruns/lastrun");

for (Element node : nodes)
{

}

Any ideas/help would be appreciated

  • 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-05T20:13:25+00:00Added an answer on June 5, 2026 at 8:13 pm

    This XSLT transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*" name="identity">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="feature[last()]">
      <xsl:call-template name="identity"/>
        <feature>3</feature>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document (corrected to be made well-formed):

    <extracts>
        <extract>
            <id>1</id>
            <features>
                <feature>1</feature>
                <feature>2</feature>
            </features>
        </extract>
    </extracts>
    

    produces the wanted, correct result:

    <extracts>
       <extract>
          <id>1</id>
          <features>
             <feature>1</feature>
             <feature>2</feature>
             <feature>3</feature>
          </features>
       </extract>
    </extracts>
    

    Explanation:

    Proper use and overriding of the identity rule.

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

Sidebar

Related Questions

using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
using (var file_stream = File.Create(users.xml)) { var serializer = new XmlSerializer(typeof(PasswordManager)); serializer.Serialize(file_stream, this); file_stream.Close();
using a binary search tree I need to add to a vector all int
I'm trying to parse a XML using Apache Commons JXPath. But for some reason,
Using Flex 3, I would like to take an image snapshot such as this:
Using C# (the .NET framework), I'm looping through all the TimeZones and their AdjustmentRules...
Using Java,I have to fetch multiple sets of values from an XML file to
using file_get_contents , I open an Internet URL and get the contents of this
I have the following data and i am using http://commons.apache.org/jxpath/ i want to directly
Using CI for the first time and i'm smashing my head with this seemingly

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.