private static UNode parseXMLElement(Element elem) {
        assert elem != null;
        
        // Get the element's content and attributes, if any.
        String content = Utils.getElementText(elem);
        NamedNodeMap attrMap = elem.getAttributes();
        
        // Map the element's child elements, if any, into a UNode list. This also tells us
        // if there are any child nodes.
        List<UNode> childUNodeList = new ArrayList<UNode>();
        boolean bDupNodeNames = parseXMLChildElems(elem, childUNodeList);
        
        // Decide what the element becomes as documented above.
        UNode result = null;
        
        // Detect base64 encoding attribute and remove it.
        String encoding = elem.getAttribute("encoding");
        if (!Utils.isEmpty(encoding) && encoding.equalsIgnoreCase("base64")) {
            attrMap.removeNamedItem("encoding");
            content = Utils.base64ToString(content);
        }
        
        // Case 1): <key>Stellar1</key>
        if (attrMap.getLength() == 0 && childUNodeList.size() == 0) {
            // No attributes and no child elements. Use the tag name and the element
            // content (which may be empty) as the node value.
            result = createValueNode(elem.getTagName(), content);
        
        // Case 2): <field name="_ID">lollapalooza</field>
        } else if (attrMap.getLength() == 1 &&
                        elem.getAttribute("name").length() > 0 &&
                        childUNodeList.size() == 0) {
            // Only a "name" attribute and no child elements. Use attribute value as the
            // name and content as the value.
            result = createValueNode(elem.getAttribute("name"), content, elem.getTagName());
            
        // Case 3): <option name="AutoTables" value="false"/>
        } else if (attrMap.getLength() == 2 &&
                        elem.getAttribute("name").length() > 0 &&
                        elem.getAttribute("value").length() > 0 &&
                        childUNodeList.size() == 0) {
            // Use values of "name" and "value" attributes. No content allowed.
            assert content.length() == 0 : "Content is not allowed for 'name/value' element: " + content;
            result = createValueNode(elem.getAttribute("name"), elem.getAttribute("value"), elem.getTagName());
            
        // Case 4): All other cases generate a MAP or an ARRAY.
        } else {
            // We shouldn't have any content for these nodes.
            assert content.length() == 0 : "Unexpected content for element '" +
                                           elem.getTagName() + "': " + content;
            
            // Use the "name" attribute value for the UNode name, if present. Otherwise,
            // use the element name.
            if (elem.getAttribute("name").length() > 0) {
                if (bDupNodeNames) {
                    result = createArrayNode(elem.getAttribute("name"), elem.getTagName());
                } else {
                    result = createMapNode(elem.getAttribute("name"), elem.getTagName());
                }
                attrMap.removeNamedItem("name");
            } else {
                if (bDupNodeNames) {
                    result = createArrayNode(elem.getTagName());
                } else {
                    result = createMapNode(elem.getTagName());
                }
            }
            
            // Map remaining attributes, if any, into child UNode objects and add all
            // child UNodes to this node's list.
            parseXMLAttributes(attrMap, childUNodeList);
            result.addChildNodes(childUNodeList);
        }
        return result;
    }
private static UNode parseXMLElement ( Element elem ) { assert elem != null ; String content = Utils . getElementText ( elem ) ; NamedNodeMap attrMap = elem . getAttributes ( ) ; List < UNode > childUNodeList = new ArrayList < UNode > ( ) ; boolean bDupNodeNames = parseXMLChildElems ( elem , childUNodeList ) ; UNode result = null ; String encoding = elem . getAttribute ( "encoding" ) ; if ( ! Utils . isEmpty ( encoding ) && encoding . equalsIgnoreCase ( "base64" ) ) { attrMap . removeNamedItem ( "encoding" ) ; content = Utils . base64ToString ( content ) ; } if ( attrMap . getLength ( ) == 0 && childUNodeList . size ( ) == 0 ) { result = createValueNode ( elem . getTagName ( ) , content ) ; } else if ( attrMap . getLength ( ) == 1 && elem . getAttribute ( "name" ) . length ( ) > 0 && childUNodeList . size ( ) == 0 ) { result = createValueNode ( elem . getAttribute ( "name" ) , content , elem . getTagName ( ) ) ; } else if ( attrMap . getLength ( ) == 2 && elem . getAttribute ( "name" ) . length ( ) > 0 && elem . getAttribute ( "value" ) . length ( ) > 0 && childUNodeList . size ( ) == 0 ) { assert content . length ( ) == 0 : "Content is not allowed for 'name/value' element: " + content ; result = createValueNode ( elem . getAttribute ( "name" ) , elem . getAttribute ( "value" ) , elem . getTagName ( ) ) ; } else { assert content . length ( ) == 0 : "Unexpected content for element '" + elem . getTagName ( ) + "': " + content ; if ( elem . getAttribute ( "name" ) . length ( ) > 0 ) { if ( bDupNodeNames ) { result = createArrayNode ( elem . getAttribute ( "name" ) , elem . getTagName ( ) ) ; } else { result = createMapNode ( elem . getAttribute ( "name" ) , elem . getTagName ( ) ) ; } attrMap . removeNamedItem ( "name" ) ; } else { if ( bDupNodeNames ) { result = createArrayNode ( elem . getTagName ( ) ) ; } else { result = createMapNode ( elem . getTagName ( ) ) ; } } parseXMLAttributes ( attrMap , childUNodeList ) ; result . addChildNodes ( childUNodeList ) ; } return result ; }
element (based on rule 2).