⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmloperator.java

📁 自己编写的几个类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//                }
//                else {
//                    elementPos = node.indexOf((Element) children.get(index));
//                }
//                System.out.println("get cur Pos is:" + elementPos);
//                node.addContent(elementPos, getNodeByDef(nodeDef));
//            }
//            return true;
//        }
//        catch (JDOMException ex) {
//            ex.printStackTrace();
//            return false;
//        }
//    }

    /**
     * 通过节点定义返回一个构造的节点
     * @param nodeDef String 节点定义字符串
     *   (如"game[@name='clouds',@location='http://clouds.com.cn',text()='this is one interesting game']"
     * @return Element 返回构造完成的节点元素
     */
    private Element getNodeByDef(String nodeDef) {
        if (nodeDef == null && nodeDef.trim().equals("")) {
            return null;
        }
        String restDef = nodeDef;
        String nodeName = null;
        Element newNode = null;
        int startPos = restDef.indexOf("[");
        if (startPos > 0) {
            nodeName = restDef.substring(0, startPos);
            newNode = new Element(nodeName);
            int endPos = restDef.indexOf("]");
            restDef = restDef.substring(startPos + 1, endPos);
            String[] attrsName = restDef.split(",");
            for (int j = 0; j < attrsName.length; j++) {
                if (attrsName[j] == null || attrsName[j].trim().equals("")) {
                    continue;
                }
                if (attrsName[j].trim().indexOf("text()=") == 0) {
                    startPos = attrsName[j].trim().indexOf("=");
                    String text = attrsName[j].trim().substring(startPos + 1).
                        trim();
                    startPos = text.indexOf("'");
                    endPos = text.lastIndexOf("'");
                    System.out.println("startpos is:" + startPos);
                    System.out.println("startpos is:" + endPos);
                    if (endPos >= 0) {
                        if (startPos == endPos) {
                            text = "";
                        }
                        else {
                            text = text.substring(startPos + 1, endPos);
                        }
                    }
                    System.out.println("get text is:" + text);
                    newNode.setText(text);
                }
                else if (attrsName[j].trim().charAt(0) == '@') {
                    startPos = attrsName[j].indexOf("=");
                    String attrName = attrsName[j].trim().substring(1, startPos).
                        trim();
                    if (attrName == null || attrName.trim().equals("")) {
                        continue;
                    }
                    String attrValue = attrsName[j].trim().substring(startPos +
                        1).trim();
                    startPos = attrValue.indexOf("'");
                    endPos = attrValue.lastIndexOf("'");
                    if (endPos >= 0) {
                        if (startPos == endPos) {
                            attrValue = "";
                        }
                        else {
                            attrValue = attrValue.substring(startPos + 1,
                                endPos);
                        }
                    }
                    newNode.setAttribute(attrName, attrValue);
                }
            }
        }
        else if (startPos < 0) {
            nodeName = restDef.trim();
            newNode = new Element(nodeName);
        }
        return newNode;
    }

    /**
     * 增加某一个元素的属性,如果属性已经存在,则只修改其对应的值.
     * @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
     * @param attrName String 属性名,
     * @param attrValue String 属性值
     * @return boolean  如果修改完成则返回true
     */
    public boolean addNodeAttribute(String xPath, String attrName,
                                    String attrValue) {
        try {
            com.zte.org.jdom.xpath.XPath xpathDom = com.zte.org.jdom.xpath.XPath.newInstance(
                xPath);
            List params = xpathDom.selectNodes(doc);
            if (params.get(0) instanceof Element) {
                Element node = (Element) params.get(0);
                node.setAttribute(attrName, attrValue);
            }
            return true;
        }
        catch (JDOMException ex) {
            ex.printStackTrace();
            return false;
        }
    }

    /**
     * 通过xpath来获取元素组
     * @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
     * @return List 返回的Vector中包含的对象是Element对象
     */
    public List getAllElement(String xPath) {
        try {
            if (doc == null)
                return new Vector();
            com.zte.org.jdom.xpath.XPath xpathDom = com.zte.org.jdom.xpath.XPath.newInstance(
                xPath);
            List params = xpathDom.selectNodes(doc);
            Vector elementList = new Vector();
            for (int i = 0; i < params.size(); i++) {
                if (params.get(i) instanceof Element) {
                    Element node = (Element) params.get(i);
                    elementList.add(node);
                }
            }
            return elementList;

        }
        catch (JDOMException ex) {
            ex.printStackTrace();
            return new Vector();
        }
    }

    /**
     * 获取符合xPath定义的第index个元素节点
     * @param xPath String  输入的xpath用于定位xml中位置,遵循xml的xpath标准
     * @param index int 指定的第index个节点
     * @return Element 如果index不合法则返回null否则返回对应的元素节点
     */
    public Element getElement(String xPath, int index) {
        List elementList = getAllElement(xPath);
        if (index < elementList.size() && index >= 0) {
            return (Element) elementList.get(index);
        }
        else {
            return null;
        }
    }

    /**
     * 获取符合xPath定义的第1个元素节点
     * @param xPath String 输入的xpath用于定位xml中位置,遵循xml的xpath标准
     * @return Element 返回第一个符合要求的元素节点,如果不存在返回null
     */
    public Element getElement(String xPath) {
        return getElement(xPath, 0);
    }

    public void testGetNodeByComm(String xpath) {
        try {
            com.zte.org.jdom.xpath.XPath xpathDom = com.zte.org.jdom.xpath.XPath.newInstance(
                xpath);
            List params = xpathDom.selectNodes(doc);
            for (int i = 0; i < params.size(); i++) {
                //如果取得的是元素节点,则说明要对text进行操作
                if (params.get(i) instanceof Element) {
                    System.out.println("get element is:" +
                                       ((Element) (params.get(i))).getName());
                }
                else if (params.get(i) instanceof com.zte.org.jdom.Comment) {
                    System.out.println("get element is:" +
                                       ((Comment) (params.get(i))).getText());
                }
            }

        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

//    public boolean shieldNode(String xPath) {
//        try {
//            com.zte.org.jdom.xpath.XPath xpathDom = com.zte.org.jdom.xpath.XPath.newInstance(
//                xPath);
//            List params = xpathDom.selectNodes(doc);
//            for (int i = 0; i < params.size(); i++) {
//                //如果取得的是元素节点,则说明要对text进行操作
//                if (params.get(i) instanceof Element) {
//                    Element node = (Element) params.get(i);
//                    Element parent = (Element) node.getParent();
//                    int pos = parent.indexOf(node);
//                    com.zte.org.jdom.output.XMLOutputter outPuter = new com.zte.org.jdom.output.
//                        XMLOutputter();
//                    java.io.OutputStream out = new java.io.
//                        ByteArrayOutputStream();
//                    try {
//                        outPuter.output(node, out);
//                        parent.addContent(pos, new Comment(out.toString()));
//                    }
//                    catch (Exception ex) {
//                        ex.printStackTrace();
//                        return false;
//                    }
//                    node.detach();
//                }
//               //如果获取到是属性对象,则说明要对属性值进行修改
//               else if (params.get(i) instanceof Attribute) {
//                   Attribute attr = (Attribute) params.get(i);
//                   attr.detach();
//               }
//            }
//            return true;
//        }
//        catch (JDOMException ex) {
//            ex.printStackTrace();
//            return false;
//        }
//    }

    /**
     * 实现接口的函数
     * @param xPath String
     * @param filterStr String
     * @param replaceStr String
     * @return boolean
     */
    public boolean modify(String xPath, String filterStr, String replaceStr) {
        return modifyNode(xPath, filterStr, replaceStr);
    }

    /**
     * 实现父接口的删除函数
     * @param xPath String
     * @return boolean
     */
    public boolean delete(String xPath) {
        return deleteNode(xPath);
    }

//    /**
//     * 实现父接口的删除函数
//     * @param xPath String
//     * @return boolean
//     */
//    public boolean shield(String xPath) {
//        return shieldNode(xPath);
//    }

    /**
     * 实现父接口的增加函数
     * @param xPath String
     * @param defStr String
     * @return boolean
     */
//    public boolean add(String xPath, String defStr) {
//        return this.addChild(xPath, defStr);
//    }

    /**
     * 实现父接口的增加函数
     * @param xPath String
     * @param value String
     * @return boolean
     */
    public boolean modify(String xPath, String value) {
        return this.modifyNode(xPath, value);
    }

    public String getAbsoluteFileName() {
        return xmlAbsoluteFile;
    }

    public String getFileName() {
        return xmlFileName;
    }

    public Document getDoc() {
        return doc;
    }

    public boolean isNewFile() {
        return isNewFile;
    }

    /**
     * 获取文件内容
     * @return String
     */
    public String toString() {
        ReadWriteFile read = new ReadWriteFile(this.getAbsoluteFileName());
        return read.readFileContent();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -