import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMForXMLParser {

    public static void main(String[] args) {
        
        String CHANELID=args[0];
        HashMap nodeMap = new HashMap();
        // DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            // 从DOM工厂获得DOM解析器
            DocumentBuilder db = dbf.newDocumentBuilder();
            // 获取XML文档
            Document doc = db.parse(new File(
                    "./APK/AndroidManifest.xml"));     //C:\Documents and Settings\Administrator\桌面\ApkTool\APK              
                    //.getResourceAsStream("TableConfig.xml"));
            // 得到XML文档的根节点
            doc.normalize(); 
            
            Element root = doc.getDocumentElement();       
            // 得到节点的子节点属性
            NamedNodeMap rootAttrMap=root.getAttributes();           
            for (int i = 0; i < rootAttrMap.getLength(); i++) {
                System.out.println("Root attribute item:  "+rootAttrMap.item(i));
            }
            // 得到节点的子节点列表
           //AndroidManifest.xml  <meta-data  android:name="UMENG_CHANNEL" 	android:value="17" />
            NodeList meta_nodes =root.getElementsByTagName("meta-data");
            for (int i = 0; i < meta_nodes.getLength(); i++){
                Node meta_node = meta_nodes.item(i);  
                Element meta_node_element=(Element) meta_node; ////将节点类型转换为XmlElement类型 
                
                if(meta_node_element.getAttribute("android:name").equals("UMENG_CHANNEL")) 
                {    
                   System.out.println(meta_node.getNodeName());
                   meta_node_element.setAttribute("android:value", CHANELID); //渠道号的值
                   NamedNodeMap info = meta_node.getAttributes();  
    
                   for (int j = 0; j < info.getLength(); j++) {  //打印meta-data节点的属性名及值
                        System.out.println("attribute item name:  "+info.item(j).getNodeName());                            
                        System.out.println("attribute item value:  "+info.item(j).getNodeValue()); 
    
                    }
                }
            }
            
            doc2XmlFile(doc,"./APK/AndroidManifest.xml");
              
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }                   
    }
    
    public static boolean doc2XmlFile(Document document,String filename) 
    { 
      boolean flag = true; 
      try 
       { 
            /** 将document中的内容写入文件中   */ 
             TransformerFactory tFactory = TransformerFactory.newInstance();    
             Transformer transformer = tFactory.newTransformer();  
            /** 编码 */ 
            //transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312"); 
             DOMSource source = new DOMSource(document);  
             StreamResult result = new StreamResult(new File(filename));    
             transformer.transform(source, result);  
         }catch(Exception ex) 
         { 
             flag = false; 
             ex.printStackTrace(); 
         } 
        return flag;       
    }

    
    
}
