XML解析以及增删改查的操作6

    有一个字符串操作类StringUtil要贴出来,之前代码里用到:

public class StringUtil {
    
    public static final String BLANKS = " \t\n";
    /**去掉首尾的空格、制表符、回车符*/
    public static String trim(String str){
        if(str==null){
            return str;
        }
        String ss = str.trim();
        for(int i=0;i<ss.length();i++){
            char c = ss.charAt(i);
            if(BLANKS.indexOf(c+"")==-1){
                ss = ss.substring(i);
                break;
            }
        }
        for(int i=ss.length()-1;i>=0;i--){
            char c = ss.charAt(i);
            if(BLANKS.indexOf(c+"")==-1){
                ss = ss.substring(0,i+1);
                break;
            }
        }
        return ss;
    }
    
    /**
     * 获取字符串str中位于两个相同字符ch之间的字符串,ch为在字符串str中出现的所有位置的前两个,例如
     * getSubStringBeetween("mimi...mi",'m') 返回字符串i
     * */
    public static String getSubStringBeetween(String str,char ch){
        String temp = str;
        int idx = temp.indexOf(ch+"");
        temp = temp.substring(idx+1);
        idx = temp.indexOf(ch+"");
        temp = temp.substring(0,idx);
        return temp;
    }
    
    /**
     * 得到字符ch在字符串src中的所有索引
     * */
    public static List<Integer> getIndexs(String src,char ch){
        List<Integer> list = new ArrayList<Integer>();
        if(src==null||src.length()==0){
            return list;
        }
        for(int i=0;i<src.length();i++){
            if(src.charAt(i)==ch){
                list.add(i);
            }
        }
        return list;
    }
    
}



下面就是写了一个测试类:

public class Test {
    public static void main(String[] args) throws IOException {
        /*Document document = new Document();
        document.setEncoding(Document.GBK);
        document.setVersion(Document.DEFAULT_VERSION);
        document.addAttribute(new Attribute("standalone", "no"));
        document.setEncoding(Document.DEFAULT_ENCODING);
        Element root = new Element();
        document.addNode(root);
        root.setName("root");
        Attribute att1 = new Attribute("id", "1");
        Attribute att2 = new Attribute("sex", "female");
        root.addAttribute(att1);
        root.addAttribute(att2);
        AnnotationNode annotationNode = new AnnotationNode("xxxx...");
        document.addNode(annotationNode);
        Element e1 = new Element();
        e1.setName("e1");
        e1.addAttribute(new Attribute("age", "100"));
//        e1.setTextComment("哈哈,e1 text...");
        TextNode textNode1 = new TextNode("哈哈  e1 text...");
        e1.addSonNode(textNode1);
        Element e2 = new Element();
        e2.setName("e2");
        e2.addAttribute(new Attribute("婚否", "未婚"));
        e2.addAttribute(new Attribute("age", "19"));
//        e2.setTextComment("e2 text...");
        TextNode textNode2 = new TextNode("哈哈  e2 text...");
        e2.addSonNode(textNode2);
        TextNode textNode3 = new TextNode("哈哈  3333 text...");
        root.addSonNode(textNode3);
        root.addSonNode(e1);
        root.addSonNode(e2);
        System.out.println(document.toString());
        System.out.println("=====================");
        System.out.println(root.getChildNodes());
        document.saveTo("c:/test/mydemo.xml");
        System.out.println("=====================");
        System.out.println(DocumentUtil.getXMLString("c:/test/mydemo.xml"));
        System.out.println("------------------------------");
        System.out.println(Arrays.toString(getElementsByTag("e1")));*/
        
        
        
        
//        Document document =DocumentUtil.parse("c:test2/ScreenSaver.XML");
        System.out.println(document);
//         Element root = document.getRootNode();
//         Set<Node> advs = root.getNodesByName("Adv");
//         Iterator<Node> it = advs.iterator();
//         for(;it.hasNext();){
//             Element node = (Element) it.next();
//             if(node.containsAttribute(new Attribute("Img", "05.png"))){
//                 node.addAttribute(new Attribute("SDate", "2013-12-16"));
//                 node.addAttribute(new Attribute("EDate", "2020-12-16"));
//                 node.addAttribute(new Attribute("btnSDate", ""));
//                 node.addAttribute(new Attribute("btnEDate", ""));
//                 break;
//             }
//         }
//         Set<Node> nodes = root.getNodesByName("mimi");
//         Node[] mimi = new Node[nodes.size()];
//         nodes.toArray(mimi);
//         Element aimimi = (Element) mimi[0];
//         TextNode aimimiNode = (TextNode) aimimi.getNodeList().get(0);
//         aimimiNode.setText("爱咪咪呀。。。");
//         document.saveTo("c:test2/ScreenSaver.XML");
        
    /
         long t1 = System.currentTimeMillis();
         Document document2 =DocumentUtil.parse("c:test2/AndroidManifest.xml");
         document2.attributeLine = Document.SINGLE_LINE;
         document2.setRootNodeAttributeLine(false);
         document2.saveTo("c:test/AndroidManifest.xml");
         System.out.println(document2);
         long t2 = System.currentTimeMillis();
         System.out.println(t2-t1);
        
    }

}



完成了!就是这么用的,可以生成xml文档,进行增删改查的操作。也可以解析xml文件。测试了一个2000多行的xml文件用了300ms,毕竟读取整篇文档进行字符串的操作挺耗性能的




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值