Hi all,

Here is the structure of my XML file.

<?xml version="1.0"?>

	<data>
		<key>468</key>
		<name>pal</name>
		<option>10</option>
	</data>

I want to read the option tag value, then increment it by 4 and write it to the same location. So at the end XML file content like this.

<?xml version="1.0"?>

	<data>
		<key>468</key>
		<name>pal</name>
		<option>14</option>
	</data>

I've read the value as follows.

public static void main(String[] args) {
        // TODO code application logic here
        try{
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            
            Document doc = docBuilder.parse (new File("records.xml"));
            
            NodeList ints = doc.getElementsByTagName("option");
            if(ints.getLength() != 0) {
                Element firstKeyElement = (Element)ints.item(0);
                NodeList firstKey = firstKeyElement.getChildNodes();
                
                String value = ((Node)firstKey.item(0)).getNodeValue().trim();
                int intValue = Integer.parseInt(value);
                System.out.println(intValue);
                
            // change the value and write it to the same location after this
 
            }
        }
        catch(SAXParseException ex){
            System.out.println(ex.getLineNumber() + ex.getMessage());
        }
        catch(SAXException ex){
            System.out.println(ex.getException());
        }
        catch(Throwable th){
            th.printStackTrace();
        }
    }

I'm stuck with how to write the new value there. Can you guys comment me on this.

Thanks

This article has been dead for over six months. Start a new discussion instead.