加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:10:13 所属栏目:百科 来源:网络整理
导读:有一个字符串操作类StringUtil要贴出来,之前代码里用到: public class StringUtil { public static final String BLANKS = " tn"; /**去掉首尾的空格、制表符、回车符*/ public static String trim(String str){ if(str==null){ return str; } String ss

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

public class StringUtil {
    
    public static final String BLANKS = " tn";
    /**去掉首尾的空格、制表符、回车符*/
    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,毕竟读取整篇文档进行字符串的操作挺耗性能的

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读