private String deleteComponentBindingFromXML( String componentXML, Integer entityId, String entityName) throws Exception { String entityNameTrigger = "Content"; if (entityName.equals(SiteNode.class.getName())) entityNameTrigger = "SiteNode"; Document document = XMLHelper.readDocumentFromByteArray(componentXML.getBytes("UTF-8")); String componentPropertyXPath = "//component/properties/property/binding[@entityId='" + entityId + "']"; // logger.info("componentPropertyXPath:" + componentPropertyXPath); String modifiedXML = null; NodeList anl = org.apache.xpath.XPathAPI.selectNodeList( document.getDocumentElement(), componentPropertyXPath); // logger.info("anl:" + anl.getLength()); for (int i = 0; i < anl.getLength(); i++) { Element component = (Element) anl.item(i); String entity = component.getAttribute("entity"); if (entity != null && entity.equalsIgnoreCase(entityNameTrigger)) { Element property = (Element) component.getParentNode(); if (property.getChildNodes().getLength() > 1) { property.removeChild(component); } else { if (property != null && property.getParentNode() != null) { property.getParentNode().removeChild(property); } } } } modifiedXML = XMLHelper.serializeDom(document, new StringBuffer()).toString(); return modifiedXML; }
private String deleteComponentFromXML(String componentXML, Integer contentId) throws Exception { Document document = XMLHelper.readDocumentFromByteArray(componentXML.getBytes("UTF-8")); String componentXPath = "//component[@contentId=" + contentId + "]"; // logger.info("componentXPath:" + componentXPath); String modifiedXML = null; NodeList anl = org.apache.xpath.XPathAPI.selectNodeList(document.getDocumentElement(), componentXPath); // logger.info("anl:" + anl.getLength()); for (int i = 0; i < anl.getLength(); i++) { Element component = (Element) anl.item(i); component.getParentNode().removeChild(component); } modifiedXML = XMLHelper.serializeDom(document, new StringBuffer()).toString(); return modifiedXML; }