Java Code Examples for org.dom4j.Element.attributeCount()

The following are Jave code examples for showing how to use attributeCount() of the org.dom4j.Element class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: iBase4J-Common   File: XmlUtil.java   View Source Code Vote up 6 votes
/**
 * Element to map
 * @param e
 * @param map
 */
public static void element2Map(Element e, Map<String, Object> map) {
    List<Object> list = e.elements();
    if (e.attributeCount() > 0) {
        for (Object attri : e.attributes()) {
            Attribute at = (Attribute)attri;
            map.put(at.getName(), at.getValue());
        }
    }
    if (list.size() < 1 && DataUtil.isEmpty(e.getText())) {
        return;
    } else if (list.size() < 1 && !DataUtil.isEmpty(e.getText())) {
        map.put("text", e.getText());
    }
    for (Object aList : list) {
        Element iter = (Element)aList;
        Map<String, Object> cMap = new HashMap<String, Object>();
        element2Map(iter, cMap);
        map.put(iter.getName(), cMap);
    }
}
 
Example 2
Project: automat   File: XmlUtil.java   View Source Code Vote up 6 votes
/**
 * Element to map
 * 
 * @param e
 * @return
 */
public static void element2Map(Element e, Map<String, Object> map) {
	List<Object> list = e.elements();
	if (e.attributeCount() > 0) {
		for (Object attri : e.attributes()) {
			Attribute at = (Attribute) attri;
			map.put(at.getName(), at.getValue());
		}
	}
	if (list.size() < 1 && DataUtil.isEmpty(e.getText())) {
		return;
	} else if (list.size() < 1 && !DataUtil.isEmpty(e.getText())) {
		map.put("text", e.getText());
	}
	for (Object aList : list) {
		Element iter = (Element) aList;
		Map<String, Object> cMap = new HashMap<String, Object>();
		element2Map(iter, cMap);
		map.put(iter.getName(), cMap);
	}
}
 
Example 3
Project: iBase4J   File: XmlUtil.java   View Source Code Vote up 6 votes
/**
 * Element to map
 * 
 * @param e
 * @return
 */
public static void element2Map(Element e, Map<String, Object> map) {
	List<Object> list = e.elements();
	if (e.attributeCount() > 0) {
		for (Object attri : e.attributes()) {
			Attribute at = (Attribute) attri;
			map.put(at.getName(), at.getValue());
		}
	}
	if (list.size() < 1 && DataUtil.isEmpty(e.getText())) {
		return;
	} else if (list.size() < 1 && !DataUtil.isEmpty(e.getText())) {
		map.put("text", e.getText());
	}
	for (Object aList : list) {
		Element iter = (Element) aList;
		Map<String, Object> cMap = new HashMap<String, Object>();
		element2Map(iter, cMap);
		map.put(iter.getName(), cMap);
	}
}
 
Example 4
Project: unitimes   File: LowercaseTableNames.java   View Source Code Vote up 6 votes
protected void writeAttributes(Element element) throws IOException {
    for (int i = 0, size = element.attributeCount(); i < size; i++) {
        Attribute attribute = element.attribute(i);
           char quote = format.getAttributeQuoteCharacter();
           if (element.attributeCount() > 2) {
               writePrintln();
               indent();
               writer.write(format.getIndent());
           } else {
           	writer.write(" ");
           }
           writer.write(attribute.getQualifiedName());
           writer.write("=");
           writer.write(quote);
           writeEscapeAttributeEntities(attribute.getValue());
           writer.write(quote);
    }
}
 
Example 5
Project: JAVA-   File: XmlUtil.java   View Source Code Vote up 6 votes
/**
 * Element to map
 * 
 * @param e
 * @return
 */
public static void element2Map(Element e, Map<String, Object> map) {
	List<Object> list = e.elements();
	if (e.attributeCount() > 0) {
		for (Object attri : e.attributes()) {
			Attribute at = (Attribute) attri;
			map.put(at.getName(), at.getValue());
		}
	}
	if (list.size() < 1 && DataUtil.isEmpty(e.getText())) {
		return;
	} else if (list.size() < 1 && !DataUtil.isEmpty(e.getText())) {
		map.put("text", e.getText());
	}
	for (Object aList : list) {
		Element iter = (Element) aList;
		Map<String, Object> cMap = new HashMap<String, Object>();
		element2Map(iter, cMap);
		map.put(iter.getName(), cMap);
	}
}
 
Example 6
Project: joai-project   File: DocMap.java   View Source Code Vote up 5 votes
/**
 *  Return true if the specified node is an Element and has either a
 *  sub-element, or an attribute (even if they are empty), OR content.
 *
 * @param  xpath  xpath to the node to be evaluated for children
 * @return        true if sub-elements, or attributes, false otherwise or if
 *      node is not an Element
 */
public boolean hasChildren(String xpath) {
	// prtln ("\nhasChildren: " + xpath);
	if (xpath == null || xpath.trim().length() == 0)
		return false;
	Node node = doc.selectSingleNode(xpath);
	if (node == null) {
		prtlnErr("\thasChildren() could not find node: (" + xpath + ")");
		return false;
	}

	if (node.getNodeType() != Node.ELEMENT_NODE) {
		prtlnErr("hasChildern() called with an non-Element - returning false");
		return false;
	}

	Element e = (Element) node;

	// we used to check for "hasText" but why would we want to do that here???
	/*
		We DO want to check in the case of an element that can contain text which ALSO
		has attributes. So we can do this check IF the typeDef is the right kind ...
	*/
	boolean hasText = (e.getTextTrim() != null && e.getTextTrim().length() > 0);
	if (hasText)
		return true;

	return (e.elements().size() > 0 ||
		e.attributeCount() > 0);
}
 
Example 7
Project: unitimes   File: LowercaseTableNames.java   View Source Code Vote up 4 votes
protected void writeElement(Element element) throws IOException {
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    writePrintln();
    indent();

    writer.write("<");
    writer.write(qualifiedName);

    boolean textOnly = true;

    for (int i = 0; i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Element) {
            textOnly = false;
        } else if (node instanceof Comment) {
            textOnly = false;
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (textOnly) {
            // we have at least one text node so lets assume
            // that its non-empty
            writeElementContent(element);
        } else {
        	if (element.attributeCount() > 3)
        		writePrintln();
            // we know it's not null or empty from above
            ++indentLevel;

            writeElementContent(element);

            --indentLevel;

            writePrintln();
            indent();
        }

        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }
   	if (element.attributeCount() > 2 && indentLevel > 0)
   		writePrintln();

    lastOutputNodeType = Node.ELEMENT_NODE;
}