List of usage examples for org.w3c.dom Node getParentNode
public Node getParentNode();
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("path/to/file.xml")); XPathFactory xFactory = XPathFactory.newInstance(); XPath xPath = xFactory.newXPath(); XPathExpression expression = xPath.compile("PersonList/Person/Age/text() | PersonList/Person/Name/text()"); NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getParentNode().getNodeName().equals("Name")) { node.setNodeValue("new name"); } else {/*from w w w .j a va2s .c om*/ node.setNodeValue("42"); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document document = dbf.newDocumentBuilder().parse(new File("input.xml")); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]"); Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE); b13Node.getParentNode().removeChild(b13Node); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(System.out)); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document document = dbf.newDocumentBuilder().parse(new File("input.xml")); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); XPathExpression expression = xpath.compile("//data/user/username[text()='simple']"); Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE); b13Node = b13Node.getParentNode(); b13Node.getParentNode().removeChild(b13Node); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(System.out)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);// w ww .ja v a 2s .c om factory.setExpandEntityReferences(false); Document doc1 = factory.newDocumentBuilder().parse(new File("filename")); NodeList list = doc1.getElementsByTagName("entry"); Element element = (Element) list.item(0); Document doc2 = factory.newDocumentBuilder().parse(new File("infilename2.xml")); // Make a copy of the element subtree suitable for inserting into doc2 Node node = doc2.importNode(element, true); // Get the parent Node parent = node.getParentNode(); // Get children NodeList children = node.getChildNodes(); // Get first child; null if no children Node child = node.getFirstChild(); // Get last child; null if no children child = node.getLastChild(); // Get next sibling; null if node is last child Node sibling = node.getNextSibling(); // Get previous sibling; null if node is first child sibling = node.getPreviousSibling(); // Get first sibling sibling = node.getParentNode().getFirstChild(); // Get last sibling sibling = node.getParentNode().getLastChild(); }
From source file:Main.java
public static void main(String[] args) throws Exception { List<String> names = new ArrayList<>(); URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265"); InputStream is = oracle.openStream(); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true);//ww w .j a v a 2 s. co m DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(is); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile("//*:*/@*"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nl = (NodeList) result; for (int i = 0; i < nl.getLength(); i++) { names.add(nl.item(i).getNodeName()); Node node = nl.item(i); String path = "." + node.getNodeName() + " = " + node.getNodeValue(); node = ((Attr) node).getOwnerElement(); while (node != null) { path = node.getNodeName() + '/' + path; node = node.getParentNode(); } System.out.println(path); } }
From source file:XMLInfo.java
public static void main(String args[]) { try {// w w w. j a v a 2 s . co m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("xmlFileName.xml"); Node root = document.getDocumentElement(); System.out.print("Here is the document's root node:"); System.out.println(" " + root.getNodeName()); System.out.println("Here are its child elements: "); NodeList childNodes = root.getChildNodes(); Node currentNode; for (int i = 0; i < childNodes.getLength(); i++) { currentNode = childNodes.item(i); System.out.println(currentNode.getNodeName()); } // get first child of root element currentNode = root.getFirstChild(); System.out.print("The first child of root node is: "); System.out.println(currentNode.getNodeName()); // get next sibling of first child System.out.print("whose next sibling is: "); currentNode = currentNode.getNextSibling(); System.out.println(currentNode.getNodeName()); // print value of next sibling of first child System.out.println("value of " + currentNode.getNodeName() + " element is: " + currentNode.getFirstChild().getNodeValue()); // print name of parent of next sibling of first child System.out.print("Parent node of " + currentNode.getNodeName() + " is: " + currentNode.getParentNode().getNodeName()); } // handle exception creating DocumentBuilder catch (ParserConfigurationException parserError) { System.err.println("Parser Configuration Error"); parserError.printStackTrace(); } // handle exception reading data from file catch (IOException fileException) { System.err.println("File IO Error"); fileException.printStackTrace(); } // handle exception parsing XML document catch (SAXException parseException) { System.err.println("Error Parsing Document"); parseException.printStackTrace(); } }
From source file:Main.java
public static void removeElement(Node elem) { elem.getParentNode().removeChild(elem); }
From source file:Main.java
/** Removes a node from its tree. */ public static void removeNode(Node node) { node.getParentNode().removeChild(node); }
From source file:Main.java
public static Node getRoot(Node node) { while (node.getParentNode() != null) node = node.getParentNode();//from www. jav a 2 s. c o m return node; }
From source file:Main.java
public static Node getNode(NodeList nl, int idx) { Node n = nl.item(idx); n.getParentNode().removeChild(n);//for performance return n;/*from w w w.ja v a 2 s . com*/ }