Here you can find the source of xmlUpdateNodeValue(String filename, String nodeName, String unitName, float ratio)
public static void xmlUpdateNodeValue(String filename, String nodeName, String unitName, float ratio)
//package com.java2s; import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { public static void xmlUpdateNodeValue(String filename, String nodeName, String unitName, float ratio) { Document docment = load(filename); Element element = docment.getDocumentElement(); NodeList items = element.getElementsByTagName(nodeName); for (int i = 0; i < items.getLength(); i++) { Element node = (Element) items.item(i); int result = (int) (getNumber(node.getFirstChild() .getNodeValue()) * ratio); if (node.getFirstChild().getNodeValue().contains(unitName)) { node.getFirstChild().setNodeValue(result + unitName); }/* w w w.ja v a 2s . c o m*/ } doc2XmlFile(docment, filename); } public static Document load(String filename) { Document document = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(new File(filename)); document.normalize(); } catch (Exception ex) { ex.printStackTrace(); } return document; } public static int getNumber(String string) { int result = 0; Matcher m; String regEx = "[^0-9]"; Pattern p = Pattern.compile(regEx); if (string != null) { m = p.matcher(string); result = Integer.valueOf(m.replaceAll("").trim()); } return result; } public static boolean doc2XmlFile(Document doc, String filename) { boolean flag = true; try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(filename)); transformer.transform(source, result); } catch (Exception ex) { flag = false; ex.printStackTrace(); } return flag; } }