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

The following are Jave code examples for showing how to use attributeIterator() 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: bulbasaur   File: BizInfo.java   View Source Code Vote up 6 votes
@SuppressWarnings("unchecked")
@Override
public StateLike parse(Element elem) {
    List<Element> infoElemList = elem.selectNodes(INFO_TAG);
    for (Element infoElem : infoElemList) {
        BizInfoElement bizInfoElement = new BizInfoElement();
        for (Iterator<Attribute> it = infoElem.attributeIterator(); it.hasNext(); ) {
            Attribute attribute = it.next();
            bizInfoElement.attribute.put(attribute.getName(), attribute.getValue());
        }
        String key = bizInfoElement.attribute.get(KEY_TAG);
        require(StringUtils.hasText(key), "attribute '" + KEY_TAG + "' in node bizInfo/info is required");
        if (bizInfoList.get(key) == null) {
            bizInfoList.put(key, new ArrayList<BizInfoElement>());
        }
        bizInfoList.get(key).add(bizInfoElement);
    }
    return this;
}
 
Example 2
Project: iBase4J-Common   File: XmlUtil.java   View Source Code Vote up 5 votes
/**
 * 将XML规范的字符串转为List对象(XML基于节点属性值的方式)
 * 
 * @param pStrXml 传入的符合XML格式规范的字符串
 * @return list 返回List对象
 */
public static final List parseXml2List(String pStrXml) {
    List lst = new ArrayList();
    String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    Document document = null;
    try {
        if (pStrXml.indexOf("<?xml") < 0) pStrXml = strTitle + pStrXml;
        document = DocumentHelper.parseText(pStrXml);
    } catch (DocumentException e) {
        logger.error("==开发人员请注意:==\n将XML格式的字符串转换为XML DOM对象时发生错误啦!" + "\n详细错误信息如下:", e);
    }
    // 获取到根节点
    Element elRoot = document.getRootElement();
    // 获取根节点的所有子节点元素
    Iterator elIt = elRoot.elementIterator();
    while (elIt.hasNext()) {
        Element el = (Element)elIt.next();
        Iterator attrIt = el.attributeIterator();
        Map map = new HashMap();
        while (attrIt.hasNext()) {
            Attribute attribute = (Attribute)attrIt.next();
            map.put(attribute.getName().toLowerCase(), attribute.getData());
        }
        lst.add(map);
    }
    return lst;
}
 
Example 3
Project: automat   File: XmlUtil.java   View Source Code Vote up 5 votes
/**
 * 将XML规范的字符串转为List对象(XML基于节点属性值的方式)
 * 
 * @param pStrXml 传入的符合XML格式规范的字符串
 * @return list 返回List对象
 */
public static final List parseXml2List(String pStrXml) {
	List lst = new ArrayList();
	String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	Document document = null;
	try {
		if (pStrXml.indexOf("<?xml") < 0)
			pStrXml = strTitle + pStrXml;
		document = DocumentHelper.parseText(pStrXml);
	} catch (DocumentException e) {
		log.error("==开发人员请注意:==\n将XML格式的字符串转换为XML DOM对象时发生错误啦!" + "\n详细错误信息如下:", e);
	}
	// 获取到根节点
	Element elRoot = document.getRootElement();
	// 获取根节点的所有子节点元素
	Iterator elIt = elRoot.elementIterator();
	while (elIt.hasNext()) {
		Element el = (Element) elIt.next();
		Iterator attrIt = el.attributeIterator();
		Map map = new HashMap();
		while (attrIt.hasNext()) {
			Attribute attribute = (Attribute) attrIt.next();
			map.put(attribute.getName().toLowerCase(), attribute.getData());
		}
		lst.add(map);
	}
	return lst;
}
 
Example 4
Project: joai-project   File: Dom4jUtils.java   View Source Code Vote up 5 votes
/**
 *  isEmpty returns true if neither the element, or any children recursively, have textual content or have a
 *  non-empty attribute.
 *
 * @param  e                 an Element to test
 * @param  ignoreWhiteSpace  determines whether whitespace is considered as textual content .
 * @return                   true if Element and all children recursively are empty
 */
public static boolean isEmpty(Element e, boolean ignoreWhiteSpace) {
	String t = e.getText();
	// if ((t == null) || (t.trim().length() > 0)) {

	// 02/28/07 no longer ignore whitespace!
	if (ignoreWhiteSpace && t.trim().length() > 0) {
		return false;
	}
	if (!ignoreWhiteSpace && t.length() > 0) {
		return false;
	}

	// is there a non-empty attribute? (note: we don't ignore whitespace for attributes ...)
	for (Iterator i = e.attributeIterator(); i.hasNext(); ) {
		Attribute a = (Attribute) i.next();
		String value = a.getValue();
		if ((value == null) || (value.trim().length() > 0)) {
			return false;
		}
	}

	// is there a non-empty child element?
	for (Iterator i = e.elementIterator(); i.hasNext(); ) {
		Element child = (Element) i.next();
		boolean isChildEmpty = isEmpty(child);
		if (!isChildEmpty) {
			return false;
		}
	}

	return true;
}
 
Example 5
Project: joai-project   File: SchemEditForm.java   View Source Code Vote up 5 votes
/**
 *  Return true if the node specified by key exists in the instance document
 *  and it has a required attribute in the instance document.
 *
 * @param  key  a jsp-encoded xpath
 * @return      The nodeExistsWithRequiredAttribute value
 */
public String getNodeExistsWithRequiredAttribute(String key) {
	String xpath = XPathUtils.decodeXPath(key);
	Node node = docMap.selectSingleNode(xpath);
	if (node == null) {
		return FALSE;
	}
	if (node.getNodeType() != Node.ELEMENT_NODE) {
		return FALSE;
	}
	Element element = (Element) node;
	if (element.attributes().isEmpty()) {
		return FALSE;
	}

	/* check attributes for a required one */
	for (Iterator i = element.attributeIterator(); i.hasNext(); ) {
		Attribute attribute = (Attribute) i.next();
		String attPath = xpath + "/@" + attribute.getQualifiedName();
		SchemaNode schemaNode = this.schemaHelper.getSchemaNode(attPath);
		if (schemaNode == null) {
			// prtln ("schemaNode not found for attribute (" + attPath + ")");
			continue;
		}
		if (schemaNode.isRequired())
			return TRUE;
	}
	return FALSE;
}
 
Example 6
Project: iBase4J   File: XmlUtil.java   View Source Code Vote up 5 votes
/**
 * 将XML规范的字符串转为List对象(XML基于节点属性值的方式)
 * 
 * @param pStrXml 传入的符合XML格式规范的字符串
 * @return list 返回List对象
 */
public static final List parseXml2List(String pStrXml) {
	List lst = new ArrayList();
	String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	Document document = null;
	try {
		if (pStrXml.indexOf("<?xml") < 0)
			pStrXml = strTitle + pStrXml;
		document = DocumentHelper.parseText(pStrXml);
	} catch (DocumentException e) {
		log.error("==开发人员请注意:==\n将XML格式的字符串转换为XML DOM对象时发生错误啦!" + "\n详细错误信息如下:", e);
	}
	// 获取到根节点
	Element elRoot = document.getRootElement();
	// 获取根节点的所有子节点元素
	Iterator elIt = elRoot.elementIterator();
	while (elIt.hasNext()) {
		Element el = (Element) elIt.next();
		Iterator attrIt = el.attributeIterator();
		Map map = new HashMap();
		while (attrIt.hasNext()) {
			Attribute attribute = (Attribute) attrIt.next();
			map.put(attribute.getName().toLowerCase(), attribute.getData());
		}
		lst.add(map);
	}
	return lst;
}
 
Example 7
Project: JAVA-   File: XmlUtil.java   View Source Code Vote up 5 votes
/**
 * 将XML规范的字符串转为List对象(XML基于节点属性值的方式)
 * 
 * @param pStrXml 传入的符合XML格式规范的字符串
 * @return list 返回List对象
 */
public static final List parseXml2List(String pStrXml) {
	List lst = new ArrayList();
	String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	Document document = null;
	try {
		if (pStrXml.indexOf("<?xml") < 0)
			pStrXml = strTitle + pStrXml;
		document = DocumentHelper.parseText(pStrXml);
	} catch (DocumentException e) {
		log.error("==开发人员请注意:==\n将XML格式的字符串转换为XML DOM对象时发生错误啦!" + "\n详细错误信息如下:", e);
	}
	// 获取到根节点
	Element elRoot = document.getRootElement();
	// 获取根节点的所有子节点元素
	Iterator elIt = elRoot.elementIterator();
	while (elIt.hasNext()) {
		Element el = (Element) elIt.next();
		Iterator attrIt = el.attributeIterator();
		Map map = new HashMap();
		while (attrIt.hasNext()) {
			Attribute attribute = (Attribute) attrIt.next();
			map.put(attribute.getName().toLowerCase(), attribute.getData());
		}
		lst.add(map);
	}
	return lst;
}
 
Example 8
Project: SimpleController   File: DatabaseXmlReader.java   View Source Code Vote up 5 votes
private List<Map<String, String>> readProperties(List<Element> propertyList) {
    List<Map<String, String>> propertyMaps=new LinkedList<>();
    for(Iterator<Element> i=propertyList.listIterator(); i.hasNext(); ){
        Element propertyElement = i.next();
        Map<String, String> propertyMap=new HashMap<>();
        for(Iterator<Attribute> j = propertyElement.attributeIterator(); j.hasNext(); ){
            Attribute attribute = j.next();
            propertyMap.put(attribute.getName(), attribute.getValue());
        }
        propertyMaps.add(propertyMap);
    }
    return propertyMaps;
}
 
Example 9
Project: SimpleController   File: DatabaseXmlReader.java   View Source Code Vote up 5 votes
private Map<String,String> readId(Element id) {
    Map<String, String> idMap=new HashMap<>();
    for(Iterator<Attribute> i = id.attributeIterator(); i.hasNext(); ){
        Attribute attribute= i.next();
        idMap.put(attribute.getName(), attribute.getValue());
    }
    return idMap;
}
 
Example 10
Project: ats-framework   File: XmlText.java   View Source Code Vote up 3 votes
/**
 * @param xpath XPath , pointing to a XML element
 * @return a HashMap , containing the attributes and their values
 * @throws XMLException
 */
@PublicAtsApi
public Map<String, String> getAttributes(
                                          String xpath ) throws XMLException {

    if (StringUtils.isNullOrEmpty(xpath)) {

        throw new XMLException("Null/empty xpath is not allowed.");

    }

    Element element = findElement(xpath);

    if (element == null) {

        throw new XMLException("'" + xpath + "' is not a valid path");

    }

    HashMap<String, String> attributes = new HashMap<>(1);

    Iterator<Attribute> it = element.attributeIterator();

    while (it.hasNext()) {
        Attribute attr = it.next();
        attributes.put(attr.getName(), attr.getValue());
    }

    return attributes;

}