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

Recommended Answers

All 4 Replies

Anyone of don't have an idea for me :)

1) Screeming for help doesn't work, we have ours lifes too. Don't we?
2) SAX tutorials www.java2s.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.