Listing E: insertBefore example
Node ndProduct = XPathAPI.selectSingleNode(doc,"/catalog/products[@sku='123']"); Node ndNewColor = doc.createElement("color"); ndNewColor.appendChild(doc.createTextNode("red")); Node ndSize = XPathAPI.selectSingleNode(doc,"/catalog/products[@sku='123']/size]"); ndProduct.insertBefore(ndSize, ndNewColor);