List of usage examples for org.w3c.dom Node removeChild
public Node removeChild(Node oldChild) throws DOMException;
oldChild
from the list of children, and returns it. From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData()))); Element element = (Element) doc.getElementsByTagName("x").item(0); Node parent = element.getParentNode(); parent.removeChild(element); parent.normalize();/*from w w w . j ava 2s .c om*/ System.out.println(parent); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// w w w. ja v a2 s . c o m factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = (Element) doc.getElementsByTagName("b").item(0); Node parent = element.getParentNode(); parent.removeChild(element); parent.normalize(); }
From source file:Main.java
public static void main(final String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w w w . ja va 2 s. c o m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(( // "<?xml version=\"1.0\"?>" + // "<people>" + // "<person><name>First Person Name</name></person>" + // "<person><name>Second Person Name</name></person>" + // "</people>" // ).getBytes())); String fragment = "<name>Changed Name</name>"; Document fragmentDoc = builder.parse(new ByteArrayInputStream(fragment.getBytes())); Node injectedNode = doc.adoptNode(fragmentDoc.getFirstChild()); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("//people/person[2]/name"); Element nodeFound = (Element) expr.evaluate(doc, XPathConstants.NODE); Node parentNode = nodeFound.getParentNode(); parentNode.removeChild(nodeFound); parentNode.appendChild(injectedNode); DOMSource domSource = new DOMSource(doc); Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(domSource, result); System.out.println(result.getWriter().toString()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// w w w. j ava2s . c o m Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml")); String fragment = "<fragment>aaa</fragment>"; factory = DocumentBuilderFactory.newInstance(); Document d = factory.newDocumentBuilder().parse(new InputSource(new StringReader(fragment))); Node node = doc.importNode(d.getDocumentElement(), true); DocumentFragment docfrag = doc.createDocumentFragment(); while (node.hasChildNodes()) { docfrag.appendChild(node.removeChild(node.getFirstChild())); } Element element = doc.getDocumentElement(); element.appendChild(docfrag); }
From source file:Main.java
public static synchronized boolean deleteNode(String charCode, Node root) { try {//w w w . jav a2 s.co m XPath xpath = XPathFactory.newInstance().newXPath(); Node node = (Node) xpath.evaluate(String.format(XPATH_EVAL_ID, charCode), root, XPathConstants.NODE); if (node != null) { // remove existing node Node parent = node.getParentNode(); parent.removeChild(node); return true; } } catch (XPathExpressionException e) { e.printStackTrace(); } return false; }
From source file:Main.java
public static void removeChildNodes(Node parent) { while (parent.hasChildNodes()) { parent.removeChild(parent.getFirstChild()); }/*ww w . j av a2 s. co m*/ }
From source file:Main.java
public static void removeAll(Node node) { while (node.getChildNodes().getLength() > 0) { node.removeChild(node.getFirstChild()); }/*from w w w .j a va 2s .c o m*/ }
From source file:Main.java
public static void removeChildren(Node node) { while (node.hasChildNodes()) { node.removeChild(node.getFirstChild()); }/* w w w.j a v a2s .c o m*/ }
From source file:Main.java
/** * Remove all children from the specified node *//* w ww . jav a2s.c o m*/ public static void removeAllChildren(Node node) { while (node.hasChildNodes()) node.removeChild(node.getLastChild()); }
From source file:Main.java
public static void removeAllChildren(Node node) { while (node.getFirstChild() != null) { node.removeChild(node.getFirstChild()); }//from w ww.java 2 s .co m }