Android XML Element Set xmlUpdate(String filename, String nodeName, String nodeAttribute, String unitName, float ratio)

Here you can find the source of xmlUpdate(String filename, String nodeName, String nodeAttribute, String unitName, float ratio)

Description

xml Update

Declaration

public static void xmlUpdate(String filename, String nodeName,
        String nodeAttribute, String unitName, float ratio) 

Method Source Code

//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 xmlUpdate(String filename, String nodeName,
            String nodeAttribute, 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.getAttribute(nodeAttribute)
                    .toString()) * ratio);
            if (node.getAttribute(nodeAttribute).toString()
                    .contains(unitName)) {
                node.setAttribute(nodeAttribute, result + unitName);
            }/*  ww w  . ja  va2  s. co  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;
    }
}

Related

  1. setText(Element parentNode, String data)