Here you can find the source of insertBeforeFirstChild(Element object, Element attrId)
public static void insertBeforeFirstChild(Element object, Element attrId)
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Element; public class Main { public static void insertBeforeFirstChild(Element object, Element attrId) { if (object.hasChildNodes()) { object.insertBefore(attrId, object.getFirstChild()); } else {//from w ww . j av a 2 s .c om object.appendChild(attrId); } } }