List of usage examples for org.w3c.dom Node getFirstChild
public Node getFirstChild();
From source file:Main.java
private static void findRecursive(Node parent, String name, List<Node> nodes, boolean onlyOne) { String nn = parent.getNodeName(); int off = nn.indexOf(':'); if (off >= 0) nn = nn.substring(off + 1);/*from ww w .j a v a 2 s . c om*/ if (nn.equals(name)) { nodes.add(parent); if (onlyOne) return; } for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { findRecursive(child, name, nodes, onlyOne); if (onlyOne && (nodes.size() > 0)) return; } }
From source file:Main.java
public static Node prependXMLChildFromString(Node parent, String child) throws ParserConfigurationException, SAXException, IOException { if (parent instanceof Document) parent = ((Document) parent).getDocumentElement(); Document newDoc = getXMLFromString(child); Node newNode = parent.getOwnerDocument().importNode(newDoc.getDocumentElement(), true); return parent.insertBefore(newNode, parent.getFirstChild()); }
From source file:filehandling.FilePreprocessor.java
public static String extractFileIdentifierFromNode(Node node) throws Exception { String fileIdentifierString = ""; Node fileIdentifier = null;// ww w. j av a 2 s . c o m if (node.getNodeType() == Node.ELEMENT_NODE) { fileIdentifier = ((Element) node).getElementsByTagName("gmd:fileIdentifier").item(0); if (null != fileIdentifier) { Node charStr = null; if (fileIdentifier.getNodeType() == Node.ELEMENT_NODE) { charStr = ((Element) fileIdentifier).getElementsByTagName("gco:CharacterString").item(0); fileIdentifierString = charStr.getFirstChild().getTextContent(); } else { throw new Exception("Invalid 'fileIdentifier' in current file!"); } } } return fileIdentifierString; }
From source file:Main.java
public static Map<String, String> XmlAsMap(Node node) { Map<String, String> map = new HashMap<String, String>(); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node currentNode = nodeList.item(i); if (currentNode.hasAttributes()) { for (int j = 0; j < currentNode.getAttributes().getLength(); j++) { Node item = currentNode.getAttributes().item(i); if (item != null) map.put(item.getNodeName(), prepare(item.getTextContent())); }// w w w . jav a 2 s . c o m } if (currentNode.getFirstChild() != null) { if (currentNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { map.putAll(XmlAsMap(currentNode)); } else if (currentNode.getFirstChild().getNodeType() == Node.TEXT_NODE) { map.put(currentNode.getLocalName(), prepare(currentNode.getTextContent())); } } } return map; }
From source file:com.mediaworx.xmlutils.XmlHelper.java
/** * Removes all empty or whitespace only text nodes from the given parent node. * @param parentNode the parent node to be cleared of empty or whitespace only text nodes *///from w ww . jav a 2 s . co m private static void removeEmptyTextNodes(Node parentNode) { Node childNode = parentNode.getFirstChild(); while (childNode != null) { // grab the "nextSibling" before the child node is removed Node nextChild = childNode.getNextSibling(); short nodeType = childNode.getNodeType(); if (nodeType == Node.TEXT_NODE) { boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty(); if (containsOnlyWhitespace) { parentNode.removeChild(childNode); } } childNode = nextChild; } }
From source file:de.itomig.itoplib.GetItopData.java
private static ItopTicket parseTicketNode(String type, Node item) { // ticket node can be Incident oder UserRequest ItopTicket ticket = new ItopTicket(type); NodeList properties = item.getChildNodes(); for (int j = 0; j < properties.getLength(); j++) { Node property = properties.item(j); String name = property.getNodeName(); if (name.equalsIgnoreCase("ref")) { ticket.setRef(property.getFirstChild().getNodeValue().trim()); } else if (name.equalsIgnoreCase("title")) { ticket.setTitle(getConcatNodeValues(property)); } else if (name.equalsIgnoreCase("priority")) { ticket.setPriority(property.getFirstChild().getNodeValue().trim()); } else if (name.equalsIgnoreCase("description")) { ticket.setDescription(getConcatNodeValues(property)); } else if (name.equalsIgnoreCase("start_date")) { ticket.setStartDate(property.getFirstChild().getNodeValue().trim()); } else if (name.equalsIgnoreCase("tto_escalation_deadline")) { ticket.setTtoEscalationDate(getConcatNodeValues(property)); } else if (name.equalsIgnoreCase("caller_id")) { ticket.setCallerID(property.getFirstChild().getNodeValue().trim()); } else if (name.equalsIgnoreCase("agent_id")) { ticket.setAgentID(property.getFirstChild().getNodeValue().trim()); } else if (name.equalsIgnoreCase("status")) { ticket.setStatus(property.getFirstChild().getNodeValue().trim()); } else if (name.equalsIgnoreCase("last_update")) { ticket.setLastUpdate(getConcatNodeValues(property)); } else if (name.equalsIgnoreCase("ticket_log")) { // itop 1.2 ticket.setTicketLog(getConcatNodeValues(property)); } else if (name.equalsIgnoreCase("public_log")) { // itop 2.0 ticket.setTicketLog(getConcatNodeValues(property)); }//w w w.j a v a 2 s . c o m } return ticket; }
From source file:Main.java
public static Iterable<Element> childrenElementsOf(final Node parent) { return new Iterable<Element>() { public Iterator<Element> iterator() { return new ElementIterator(); }//from w w w. j av a2 s .c o m class ElementIterator implements Iterator<Element> { private Node continueFrom; private Element curNode; ElementIterator() { continueFrom = parent.getFirstChild(); fetch(); } public boolean hasNext() { return curNode != null; } public Element next() { Element res = curNode; fetch(); return res; } public void remove() { throw new UnsupportedOperationException("remove"); } private void fetch() { while (!(continueFrom == null || continueFrom instanceof Element)) { continueFrom = continueFrom.getNextSibling(); } if (continueFrom != null) { curNode = (Element) continueFrom; continueFrom = curNode.getNextSibling(); } else { curNode = null; } } } }; }
From source file:Main.java
public static void stepThrough(Node start) { System.out.println(start.getNodeName() + " = " + start.getNodeValue()); if (start.getNodeType() == Element.ELEMENT_NODE) { NamedNodeMap startAttr = start.getAttributes(); for (int i = 0; i < startAttr.getLength(); i++) { Node attr = startAttr.item(i); System.out.println(" Attribute: " + attr.getNodeName() + " = " + attr.getNodeValue()); }/* w w w. j av a 2 s .c o m*/ } for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) { stepThrough(child); } }
From source file:com.mediaworx.xmlutils.XmlHelper.java
/** * Removes text nodes that are empty or contain whitespace only if the parent node has at least one child of any * of the following types: ELEMENT, CDATA, COMMENT. This is used to improve the XML format when using a transformer * to do the formatting (whitespace nodes are interfering with indentation and line breaks). * This method was modeled after a method by "user2401669" found on * <a href="http://stackoverflow.com/questions/16641835/strange-xml-indentation">StackOverflow</a>. *//*from ww w . ja va 2 s .c o m*/ public static void cleanEmptyTextNodes(Node parentNode) { boolean removeEmptyTextNodes = false; Node childNode = parentNode.getFirstChild(); while (childNode != null) { short nodeType = childNode.getNodeType(); if (nodeType == Node.ELEMENT_NODE || nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.COMMENT_NODE) { removeEmptyTextNodes = true; if (nodeType == Node.ELEMENT_NODE) { cleanEmptyTextNodes(childNode); // recurse into subtree } } childNode = childNode.getNextSibling(); } if (removeEmptyTextNodes) { removeEmptyTextNodes(parentNode); } }
From source file:Main.java
public static Node duplicate(Node node, Document ownerDocument) { if (node instanceof Text) return ownerDocument.createTextNode(node.getNodeValue()); Node newNode = ownerDocument.createElement(node.getNodeName()); NamedNodeMap attribs = node.getAttributes(); for (int i = 0; i < attribs.getLength(); i++) { Node attrib = attribs.item(i); addAttribute(newNode, attrib.getNodeName(), attrib.getNodeValue()); }//w w w. j av a 2 s. c om for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) { Node newN = duplicate(n, ownerDocument); newNode.appendChild(newN); } return newNode; }