Java Code Examples for org.dom4j.Element#isTextOnly()

The following examples show how to use org.dom4j.Element#isTextOnly() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: ats-framework   File: XmlText.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param xpath XPath , pointing to a XML element
 * @return a float XML value
 * @throws XMLException
 */
@PublicAtsApi
public float getFloat(
                       String xpath ) throws XMLException {

    Object object = get(xpath);

    Element root = ((XmlText) object).root;

    if (root.isTextOnly()) {

        object = root.getText().trim();

    } else {

        object = "";

    }

    try {
        return Float.parseFloat( ((String) object).trim());
    } catch (NumberFormatException nfe) {
        throw new XMLException("'" + xpath + "' does not point to a float value:\n"
                               + object.toString());
    }
}
 
Example 2
Source Project: onos   File: DefaultXmlCodecWalker.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void walk(XmlListener listener, Element element,
                 Element rootElement) {
    try {
        Element newElement = element.createCopy();
        newElement.remove(element.getNamespace());

        listener.enterXmlElement(element, getElementType(newElement),
                                 rootElement);

        if (element.hasContent() && !element.isTextOnly()) {
            for (Iterator i = element.elementIterator(); i.hasNext();) {
                Element childElement = (Element) i.next();
                walk(listener, childElement, rootElement);
            }
        }

        listener.exitXmlElement(element, getElementType(element),
                                rootElement);
    } catch (Exception e) {
        log.error("Exception occurred when walk xml element: {}", element);
    }
}
 
Example 3
Source Project: ats-framework   File: XmlText.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param xpath XPath , pointing to a XML element
 * @return a boolean XML value
 * @throws XMLException
 */
@PublicAtsApi
public boolean getBoolean(
                           String xpath ) throws XMLException {

    Object object = get(xpath);

    Element root = ((XmlText) object).root;

    if (root.isTextOnly()) {

        object = root.getText().trim();

    } else {

        object = "";

    }

    if ("true".equalsIgnoreCase((String) object)
        || "false".equalsIgnoreCase((String) object)) {
        return Boolean.parseBoolean( ((String) object).trim());
    } else {
        throw new XMLException("'" + xpath + "' does not point to an boolean value:\n"
                               + object.toString());
    }
}
 
Example 4
Source Project: onos   File: NetconfCodec.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the data root element based on the NETCONF operation parameter.
 *
 * @param rootElement root element of document tree to find the root node
 * @param opType      protocol operation being performed
 * @return the data root node element
 */
public Element getDataRootElement(Element rootElement,
                                  YmsOperationType opType) {

    Element retElement = null;
    String elementName = rootElement.getName();
    try {
        validateOpType(elementName, opType);
        // If config tag name is found then set the root element node.
        if (DATA.equals(elementName)
                || CONFIG.equals(elementName)
                || FILTER.equals(elementName)) {
            return rootElement;
        }

        // If element has child node then traverse through the child node
        // by recursively calling getDataRootElement method.
        if (rootElement.hasContent() && !rootElement.isTextOnly()) {
            for (Iterator i = rootElement.elementIterator();
                 i.hasNext();) {
                Element childElement = (Element) i.next();
                retElement = getDataRootElement(childElement, opType);
            }
        }
    } catch (Exception e) {
        // TODO
    }

    return retElement;
}
 
Example 5
@SuppressWarnings("unchecked")
private  Map<String, Object> createMap(Element element) {
	Map<String, Object> map = new HashMap<String, Object>();
	for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
		Node currentNode = element.node(i);
		if ( currentNode instanceof Element ) {
			Element currentElement = (Element)currentNode;
			String key = currentElement.getName();
			Object toAdd = null;
			if (currentElement.isTextOnly()) {
				 toAdd = currentElement.getStringValue();
			} else {
				toAdd = createMap(currentElement);
			}
			if (map.containsKey(key)) {
				Object value = map.get(key);
				List listOfValues = new ArrayList<Object>();
				if (value instanceof List) {
					listOfValues = (List<Object>)value;
				} else {
					listOfValues.add(value);
				}
				listOfValues.add(toAdd);
				map.put(key, listOfValues);
			} else {
				map.put(key, toAdd);
			}
		}
	}
	return map;
}
 
Example 6
Source Project: anyline   File: DataRow.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 解析xml
    * @param keyCase KEY_CASE
    * @param element element
 * @return return
 */
public static DataRow parseXml(KEY_CASE keyCase, Element element){
	DataRow row = new DataRow(keyCase);
	if(null == element){
		return row;
	}
	Iterator<Element> childs=element.elementIterator();
	String key = element.getName();
	String namespace = element.getNamespacePrefix();
	if(BasicUtil.isNotEmpty(namespace)){
		key = namespace + ":" + key;
	}
	if(element.isTextOnly() || !childs.hasNext()){
		row.put(key, element.getTextTrim());
	}else{
		while(childs.hasNext()){
			Element child = childs.next();
			String childKey = child.getName();
			String childNamespace = child.getNamespacePrefix();
			if(BasicUtil.isNotEmpty(childNamespace)){
				childKey = childNamespace + ":" + childKey;
			}
			if(child.isTextOnly() || !child.elementIterator().hasNext()){
				row.put(childKey, child.getTextTrim());
				continue;
			}
			DataRow childRow = parseXml(keyCase,child);
			Object childStore = row.get(childKey);
			if(null == childStore){
				row.put(childKey, childRow);
			}else{
				if(childStore instanceof DataRow){
					DataSet childSet = new DataSet();
					childSet.add((DataRow)childStore);
					childSet.add(childRow);
					row.put(childKey, childSet);
				}else if(childStore instanceof DataSet){
					((DataSet)childStore).add(childRow);
				}
			}
		}	
	}
	Iterator<Attribute> attrs = element.attributeIterator();
	while(attrs.hasNext()){
		Attribute attr = attrs.next();
		row.attr(attr.getName(), attr.getValue());
	}
	return row;
}
 
Example 7
Source Project: ats-framework   File: XmlText.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @param xpath XPath , pointing to a XML element
 * @return a String XML value
 * @throws XMLException
 */
@PublicAtsApi
public String getString(
                         String xpath ) throws XMLException {

    Object object = get(xpath);

    Element root = ((XmlText) object).root;

    if (root.isTextOnly()) {

        object = root.getText().trim();

    } else {

        throw new XMLException("'" + xpath + "' does not point to a String value:\n"
                               + object.toString());

    }

    return (String) object;
}
 
Example 8
Source Project: ats-framework   File: XmlText.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @param xpath XPath , pointing to a XML element
 * @return an Integer XML value
 * @throws XMLException
 */
@PublicAtsApi
public int getInt(
                   String xpath ) throws XMLException {

    Object object = get(xpath);

    Element root = ((XmlText) object).root;

    if (root.isTextOnly()) {

        object = root.getText().trim();

    } else {

        object = "";

    }

    try {
        return Integer.parseInt( ((String) object).trim());
    } catch (NumberFormatException nfe) {
        throw new XMLException("'" + xpath + "' does not point to an int value:\n" + object.toString());
    }
}
 
Example 9
Source Project: onos   File: DefaultXmlCodecWalker.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Determine the type of an element.
 *
 * @param element to be analysed
 * @return type of the element
 */
private XmlNodeType getElementType(Element element) {
    return element.hasContent() && element.isTextOnly() ?
            TEXT_NODE : OBJECT_NODE;
}