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
This XSLT transformation:
when applied on the provided XML document (corrected to be made well-formed):
produces the wanted, correct result:
Explanation:
Proper use and overriding of the identity rule.