dimens tool---quick value

本文介绍了一种使用Java实现的XML文件节点值批量更新的方法。通过解析XML文件,并针对以dp和sp结尾的节点值进行缩放操作,然后将修改后的XML文档保存到新的文件中。此方法适用于需要调整UI尺寸的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

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.Node;
import org.w3c.dom.NodeList;
public class UpdateXml {
	static float x_scal=0;
	static float y_scal=0;
 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;       
    }
 
 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 void xmlUpdateDemo_x() 
    {  
	   String path = "./src/dimens_x.xml"; 
       Document document = load(path); 
       Node root=document.getDocumentElement(); 
      /** 如果root有子元素 */ 
      if(root.hasChildNodes()) 
       { 
         /** ftpnodes */ 
          NodeList ftpnodes = root.getChildNodes(); 
         /** 循环取得ftp所有节点 */ 
         for (int i=0;i<ftpnodes.getLength();i++) 
          {                       
             NodeList ftplist = ftpnodes.item(i).getChildNodes(); 
             
            for (int k=0;k<ftplist.getLength();k++) 
             { 
            	
               Node subnode = ftplist.item(k); 
               if(null == subnode)continue;
               
               
               String strValue=subnode.getNodeValue();
               System.out.println(strValue);
               if(strValue.endsWith("dp"))
            {
            	 

                	  String strTrue= strValue.replace("dp","");
//                	  System.out.println(strTrue);
                	 double fTrue=Float.valueOf(strTrue.trim());
                	 fTrue=(double) (fTrue*x_scal);
                	BigDecimal   b   =   new   BigDecimal(fTrue);  
                 double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
                	
                	 System.out.println(fTrue);
                	 
                	 subnode.setNodeValue(f1+"dp");
            }
               if(strValue.endsWith("sp"))
               {
               	 

                   	  String strTrue= strValue.replace("sp","");
//                   	  System.out.println(strTrue);
                   	 double fTrue=Float.valueOf(strTrue.trim());
                   	 fTrue=(double) (fTrue*y_scal);
                   	BigDecimal   b   =   new   BigDecimal(fTrue);  
                    double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
                   	
                   	 System.out.println(fTrue);
                   	 
                   	 subnode.setNodeValue(f1+"sp");
               }
             } 
             
          } 
       } 
      String path2="./src/dimens_x_changed.xml"; 
      File file=new File(path2);
      if(!file.exists())
      {
    	  try {
			file.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      doc2XmlFile(document,path2); 
    }
   public static void xmlUpdateDemo_y() 
   {  
	   String path = "./src/dimens_y.xml"; 
      Document document = load(path); 
      Node root=document.getDocumentElement(); 
     /** 如果root有子元素 */ 
     if(root.hasChildNodes()) 
      { 
        /** ftpnodes */ 
         NodeList ftpnodes = root.getChildNodes(); 
        /** 循环取得ftp所有节点 */ 
        for (int i=0;i<ftpnodes.getLength();i++) 
         {                       
            NodeList ftplist = ftpnodes.item(i).getChildNodes(); 
            
           for (int k=0;k<ftplist.getLength();k++) 
            { 
           	
              Node subnode = ftplist.item(k); 
              if(null == subnode)continue;
              
              
              String strValue=subnode.getNodeValue();
              System.out.println(strValue);
              if(strValue.endsWith("dp"))
           {
           	 
           	  String strTrue= strValue.replace("dp","");
//           	  System.out.println(strTrue);
           	 double fTrue=Float.valueOf(strTrue.trim());
           	 fTrue=(double) (fTrue*y_scal);
           	BigDecimal   b   =   new   BigDecimal(fTrue);  
            double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
           	
           	 System.out.println(fTrue);
           	 
           	 subnode.setNodeValue(f1+"dp");
           }
              if(strValue.endsWith("sp"))
              {
            	  String strTrue= strValue.replace("sp","");
//           	  System.out.println(strTrue);
           	 double fTrue=Float.valueOf(strTrue.trim());
           	 fTrue=(double) (fTrue*y_scal);
           	BigDecimal   b   =   new   BigDecimal(fTrue);  
            double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
           	
           	 System.out.println(fTrue);
           	 
           	 subnode.setNodeValue(f1+"sp");
              }
              
              
              
             /** 删除ftp-chn节点 */ 
//             if (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="ftp-chn") 
//              { 
//                 ftpnodes.item(i).removeChild(subnode); 
//              } 
             /** 修改ftp-host的值为 192.168.0.1 */ 
//             if (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="status") 
//              {                  
//                 subnode.getFirstChild().setNodeValue("1"); 
//              } 
            } 
            
         } 
      } 
     String path2="./src/dimens_y_changed.xml"; 
     File file=new File(path2);
     if(!file.exists())
     {
   	  try {
			file.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     }
     doc2XmlFile(document,path2); 
   }
   
   public static void main(String args[])throws Exception
   {
	   UpdateXml.x_scal=(float) 0.711;
	   UpdateXml.y_scal=(float) 0.711;
	   
	   UpdateXml.xmlUpdateDemo_x();
       UpdateXml.xmlUpdateDemo_y();
   }
} 



/***
 *    
               String strValue=subnode.getNodeValue();
               System.out.println(strValue);
               if(strValue.endsWith("dp"))
            {
            	 
            	  String strTrue= strValue.replace("dp","");
            	  System.out.println(strTrue);
            	 float fTrue=Float.valueOf(strTrue.trim());
            	 fTrue=(float) (fTrue*0.445);
            	 System.out.println(fTrue);
            	 
            	 subnode.getFirstChild().setNodeValue(fTrue+"dp");
            	 System.out.println(strTrue);
            }
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值