0

I have an xml file with the following structure:

<jnlp>
 <resource>
   <pattern>
     <file>cisco-upgrade_v1_5.jar</file>
     <version-id>1.5</version-id>
   </pattern>
 </resource>
 <resource>
   <pattern>
     <file>commons-email_v1_1.jar</file>
     <version-id>1.1</version-id>
   </pattern>
 </resource>
 <resource>
   <pattern>
     <file>cnt_v1_1.jar</file>
     <version-id>1.1</version-id>
   </pattern>
 </resource>
</jnlp

What I am trying to do is modify the version-id for only one of the resources. What would be the best way to go about doing this? Is this possible to do without each resource having an ID?

1
  • Use any of the XML processing API's available for java. As for if its possible without the resource ID, only if you can know for certain which order the resources will appear in before hand. Commented May 9, 2014 at 18:41

2 Answers 2

2

Pretty easy using the default document classes provided by the JDK. I would prefer to use XPath on this particular item because it is easy to adapt to changes in the DOM.

First parse your document and place in a org.w3c.dom.Document class

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new File("path to your file"));

Then I would create my XPath object that will get the version number to the item I want to change. This XPath query will find the version node where a sibling node named files value is set to cisco-upgrade_v1_5.jar.

XPath xPath = XPathFactory.newInstance().newXPath();
Node ciscoVersion = (Node)xPath.evaluate("/jnlp/resource/pattern/version-id[../file='cisco-upgrade_v1_5.jar']", 
    document.getDocumentElement(), 
    XPathConstants.NODE);

Set the new version number

ciscoVersion.setTextContent("1.5.2");

So now we have the version number changed for the cisco-upgrade_v1_5.jar entry we can output our result

TransformerFactory tranFactory = TransformerFactory.newInstance();  
Transformer aTransformer = tranFactory.newTransformer();  
Source src = new DOMSource(document);  
Result dest = new StreamResult(new FileOutputStream(new File("path to your file")));  
aTransformer.transform(src, dest);  

For convenience here is a list of imports you will need to implement this

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
Sign up to request clarification or add additional context in comments.

Comments

0

One of the best way, i know is using the SAX Parser.

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

Yes, we can implement without id, but not reference.

I mean to say, we can even fetch the element using

Node nodeRef  doc.getElementsByTagName("pattern")[0];// fetches the first record.

NodeList nodes = nodeRef.getChildNodes();//loop the nodes and find the matches to replace the text.

Using Node we can access the Elements, where you will have the scope to change the content of the element.

Read more about - Node, NodeList,NamedNodeMap ,Element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.