private boolean addPropertyElement ( Element element , String elementName , String textContent ) { Document doc = element . getOwnerDocument ( ) ; Element newElement = doc . createElement ( elementName ) ; newElement . setTextContent ( textContent ) ; Text textNode = doc . createTextNode ( "\n " ) ; NodeList childNodes = element . getChildNodes ( ) ; for ( int i = 0 , size = childNodes . getLength ( ) ; i < size ; i ++ ) { Node item = childNodes . item ( i ) ; if ( item instanceof Element ) { Element childElement = ( Element ) item ; int value = childElement . getTagName ( ) . compareTo ( elementName ) ; if ( value == 0 ) { return false ; } if ( value > 0 ) { element . insertBefore ( textNode , childElement ) ; element . insertBefore ( newElement , textNode ) ; return true ; } } } // lets insert at the end element . appendChild ( textNode ) ; element . appendChild ( newElement ) ; return true ; }
Lets add a child element at the right place