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

📄 abstractelement.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

        return list.iterator();
    }

    public Iterator elementIterator(QName qName) {
        List list = elements(qName);

        return list.iterator();
    }

    public Iterator elementIterator(String name, Namespace ns) {
        return elementIterator(getDocumentFactory().createQName(name, ns));
    }

    // Attribute methods
    // -------------------------------------------------------------------------
    public List attributes() {
        return new ContentListFacade(this, attributeList());
    }

    public Iterator attributeIterator() {
        return attributeList().iterator();
    }

    public Attribute attribute(int index) {
        return (Attribute) attributeList().get(index);
    }

    public int attributeCount() {
        return attributeList().size();
    }

    public Attribute attribute(String name) {
        List list = attributeList();

        int size = list.size();

        for (int i = 0; i < size; i++) {
            Attribute attribute = (Attribute) list.get(i);

            if (name.equals(attribute.getName())) {
                return attribute;
            }
        }

        return null;
    }

    public Attribute attribute(QName qName) {
        List list = attributeList();

        int size = list.size();

        for (int i = 0; i < size; i++) {
            Attribute attribute = (Attribute) list.get(i);

            if (qName.equals(attribute.getQName())) {
                return attribute;
            }
        }

        return null;
    }

    public Attribute attribute(String name, Namespace namespace) {
        return attribute(getDocumentFactory().createQName(name, namespace));
    }

    /**
     * This method provides a more optimal way of setting all the attributes on
     * an Element particularly for use in {@link org.dom4j.io.SAXReader}.
     * 
     * @param attributes
     *            DOCUMENT ME!
     * @param namespaceStack
     *            DOCUMENT ME!
     * @param noNamespaceAttributes
     *            DOCUMENT ME!
     */
    public void setAttributes(Attributes attributes,
            NamespaceStack namespaceStack, boolean noNamespaceAttributes) {
        // now lets add all attribute values
        int size = attributes.getLength();

        if (size > 0) {
            DocumentFactory factory = getDocumentFactory();

            if (size == 1) {
                // allow lazy construction of the List of Attributes
                String name = attributes.getQName(0);

                if (noNamespaceAttributes || !name.startsWith("xmlns")) {
                    String attributeURI = attributes.getURI(0);

                    String attributeLocalName = attributes.getLocalName(0);

                    String attributeValue = attributes.getValue(0);

                    QName attributeQName = namespaceStack.getAttributeQName(
                            attributeURI, attributeLocalName, name);

                    add(factory.createAttribute(this, attributeQName,
                            attributeValue));
                }
            } else {
                List list = attributeList(size);

                list.clear();

                for (int i = 0; i < size; i++) {
                    // optimised to avoid the call to attribute(QName) to
                    // lookup an attribute for a given QName
                    String attributeName = attributes.getQName(i);

                    if (noNamespaceAttributes
                            || !attributeName.startsWith("xmlns")) {
                        String attributeURI = attributes.getURI(i);

                        String attributeLocalName = attributes.getLocalName(i);

                        String attributeValue = attributes.getValue(i);

                        QName attributeQName = namespaceStack
                                .getAttributeQName(attributeURI,
                                        attributeLocalName, attributeName);

                        Attribute attribute = factory.createAttribute(this,
                                attributeQName, attributeValue);

                        list.add(attribute);

                        childAdded(attribute);
                    }
                }
            }
        }
    }

    public String attributeValue(String name) {
        Attribute attrib = attribute(name);

        if (attrib == null) {
            return null;
        } else {
            return attrib.getValue();
        }
    }

    public String attributeValue(QName qName) {
        Attribute attrib = attribute(qName);

        if (attrib == null) {
            return null;
        } else {
            return attrib.getValue();
        }
    }

    public String attributeValue(String name, String defaultValue) {
        String answer = attributeValue(name);

        return (answer != null) ? answer : defaultValue;
    }

    public String attributeValue(QName qName, String defaultValue) {
        String answer = attributeValue(qName);

        return (answer != null) ? answer : defaultValue;
    }

    /**
     * DOCUMENT ME!
     * 
     * @param name
     *            DOCUMENT ME!
     * @param value
     *            DOCUMENT ME!
     * 
     * @deprecated As of version 0.5. Please use {@link
     *             #addAttribute(String,String)} instead. WILL BE REMOVED IN
     *             dom4j-1.6 !!
     */
    public void setAttributeValue(String name, String value) {
        addAttribute(name, value);
    }

    /**
     * DOCUMENT ME!
     * 
     * @param qName
     *            DOCUMENT ME!
     * @param value
     *            DOCUMENT ME!
     * 
     * @deprecated As of version 0.5. Please use {@link
     *             #addAttribute(String,String)} instead. WILL BE REMOVED IN
     *             dom4j-1.6 !!
     */
    public void setAttributeValue(QName qName, String value) {
        addAttribute(qName, value);
    }

    public void add(Attribute attribute) {
        if (attribute.getParent() != null) {
            String message = "The Attribute already has an existing parent \""
                    + attribute.getParent().getQualifiedName() + "\"";

            throw new IllegalAddException(this, attribute, message);
        }

        if (attribute.getValue() == null) {
            // try remove a previous attribute with the same
            // name since adding an attribute with a null value
            // is equivalent to removing it.
            Attribute oldAttribute = attribute(attribute.getQName());

            if (oldAttribute != null) {
                remove(oldAttribute);
            }
        } else {
            attributeList().add(attribute);

            childAdded(attribute);
        }
    }

    public boolean remove(Attribute attribute) {
        List list = attributeList();

        boolean answer = list.remove(attribute);

        if (answer) {
            childRemoved(attribute);
        } else {
            // we may have a copy of the attribute
            Attribute copy = attribute(attribute.getQName());

            if (copy != null) {
                list.remove(copy);

                answer = true;
            }
        }

        return answer;
    }

    // Processing instruction API
    // -------------------------------------------------------------------------
    public List processingInstructions() {
        List list = contentList();

        BackedList answer = createResultList();

        int size = list.size();

        for (int i = 0; i < size; i++) {
            Object object = list.get(i);

            if (object instanceof ProcessingInstruction) {
                answer.addLocal(object);
            }
        }

        return answer;
    }

    public List processingInstructions(String target) {
        List list = contentList();

        BackedList answer = createResultList();

        int size = list.size();

        for (int i = 0; i < size; i++) {
            Object object = list.get(i);

            if (object instanceof ProcessingInstruction) {
                ProcessingInstruction pi = (ProcessingInstruction) object;

                if (target.equals(pi.getName())) {
                    answer.addLocal(pi);
                }
            }
        }

        return answer;
    }

    public ProcessingInstruction processingInstruction(String target) {
        List list = contentList();

        int size = list.size();

        for (int i = 0; i < size; i++) {
            Object object = list.get(i);

            if (object instanceof ProcessingInstruction) {
                ProcessingInstruction pi = (ProcessingInstruction) object;

                if (target.equals(pi.getName())) {
                    return pi;
                }
            }
        }

        return null;
    }

    public boolean removeProcessingInstruction(String target) {
        List list = contentList();

        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object object = iter.next();

            if (object instanceof ProcessingInstruction) {
                ProcessingInstruction pi = (ProcessingInstruction) object;

                if (target.equals(pi.getName())) {
                    iter.remove();

                    return true;
                }
            }
        }

        return false;
    }

    // Content Model methods
    // -------------------------------------------------------------------------
    public Node getXPathResult(int index) {
        Node answer = node(index);

        if ((answer != null) && !answer.supportsParent()) {
            return answer.asXPathResult(this);
        }

        return answer;
    }

    public Element addAttribute(String name, String value) {
        // adding a null value is equivalent to removing the attribute
        Attribute attribute = attribute(name);

        if (value != null) {
            if (attribute == null) {
                add(getDocumentFactory().createAttribute(this, name, value));
            } else if (attribute.isReadOnly()) {
                remove(attribute);

                add(getDocumentFactory().createAttribute(this, name, value));
            } else {
                attribute.setValue(value);
            }
        } else if (attribute != null) {
            remove(attribute);
        }

        return this;
    }

    public Element addAttribute(QName qName, String value) {
        // adding a null value is equivalent to removing the attribute
        Attribute attribute = attribute(qName);

        if (value != null) {
            if (attribute == null) {
                add(getDocumentFactory().createAttribute(this, qName, value));
            } else if (attribute.isReadOnly()) {
                remove(attribute);

                add(getDocumentFactory().createAttribute(this, qName, value));
            } else {
                attribute.setValue(value);
            }
        } else if (attribute != null) {
            remove(attribute);
        }

        return this;
    }

    public Element addCDATA(String cdata) {
        CDATA node = getDocumentFactory().createCDATA(cdata);

        addNewNode(node);

        return this;
    }

    public Element addComment(String comment) {
        Comment node = getDocumentFactory().createComment(comment);

        addNewNode(node);

        return this;
    }

    public Element addElement(String name) {
        DocumentFactory factory = getDocumentFactory();

        int index = name.indexOf(":");

        String prefix = "";

        String localName = name;

        Namespace namespace = null;

        if (index > 0) {
            prefix = name.substring(0, index);

            localName = name.substring(index + 1);

            namespace = getNamespaceForPrefix(prefix);

            if (namespace == null) {
                throw new IllegalAddException("No such namespace prefix: "
                        + prefix + " is in scope on: " + this
                        + " so cannot add element: " + name);
            }

⌨️ 快捷键说明

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