List of usage examples for org.w3c.dom Node normalize
public void normalize();
Text
nodes in the full depth of the sub-tree underneath this Node
, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text
nodes, i.e., there are neither adjacent Text
nodes nor empty Text
nodes. 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);//w w w. j a v a 2 s. co m parent.normalize(); 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 . j a v a 2 s. c om 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 String getNodeValue(Node node) { if (node == null) { return null; } else if (node instanceof Text) { return node.getNodeValue().trim(); } else if (node instanceof Element) { node.normalize(); Node temp = node.getFirstChild(); if (temp != null && (temp instanceof Text)) return temp.getNodeValue().trim(); else//from w w w. j a v a 2 s.c o m return ""; } else { return node.getNodeValue().trim(); } }
From source file:Main.java
public static final String createStringFromDOMNode(Node node, boolean omitDeclaration) { assert node != null; StringWriter out = null;// w ww .j a v a2 s .co m try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); node.normalize(); Source source = new DOMSource(node); out = new StringWriter(); Result resultStream = new StreamResult(out); if (omitDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } transformer.transform(source, resultStream); } catch (Exception e) { } if (out != null) { return out.toString(); } return null; }
From source file:de.betterform.xml.dom.DOMUtil.java
/** * gets the first child of a node which is a text or cdata node. *//*from ww w. j a v a 2 s. c o m*/ public static Node getTextNode(Node start) { Node n = null; start.normalize(); NodeList nl; if (start.getNodeType() == Node.DOCUMENT_NODE) { nl = ((Document) start).getDocumentElement().getChildNodes(); } else { nl = start.getChildNodes(); } int len = nl.getLength(); if (len == 0) { return null; } for (int i = 0; i < len; i++) { n = nl.item(i); if (n.getNodeType() == Node.TEXT_NODE) { return n; } else if (n.getNodeType() == Node.CDATA_SECTION_NODE) { return n; } } return null; }
From source file:com.oracle.tutorial.jdbc.ProductInformationTable.java
public void populateTable(String fileName) throws SQLException, ParserConfigurationException, SAXException, IOException, XPathExpressionException { javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); // factory.setNamespaceAware(true); factory.setNamespaceAware(true);//from w w w . j a va2 s. co m DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(fileName); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xPath = xPathfactory.newXPath(); NodeList nodes = (NodeList) xPath.evaluate("/coffee-product-information/item[coffee = 'Columbian']", doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Node currentNode = nodes.item(i); // Retrieve the description element currentNode.normalize(); if (currentNode == null) { System.out.println("Current node is null"); } // System.out.println(currentNode.getTextContent()); Node descriptionNode = (Node) xPath.evaluate("description", currentNode, XPathConstants.NODE); if (descriptionNode == null) { System.out.println("DescriptionNode is null"); } else { System.out.println(descriptionNode.getTextContent()); NodeList descriptionNodeChildren = descriptionNode.getChildNodes(); System.out.println("Description node has " + descriptionNodeChildren.getLength() + " child nodes"); Node descNodeChild = descriptionNode.getFirstChild(); System.out.println("Only child node type: " + descNodeChild.getNodeType()); } // System.out.println("Description: " + descriptionNode.getNodeValue()); // System.out.println(nodes.item(i).getNodeValue()); } }
From source file:com.adaptris.util.text.xml.XPath.java
/** * returns an array of string values taken from a list of elements returned by * an xpath// w w w. j a va2 s. c o m * * @param context the node to apply the XPath to * @param xpath the xpath to apply * @return the strings extracted * @throws XPathExpressionException on error */ public String[] selectMultipleTextItems(Node context, String xpath) throws XPathExpressionException { NodeList list = selectNodeList(context, xpath); String[] retArray = new String[list.getLength()]; for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node != null) { if (node.getNodeType() == Node.ATTRIBUTE_NODE) { retArray[i] = node.getNodeValue(); } else if (node.getNodeType() == Node.TEXT_NODE) { retArray[i] = node.getNodeValue(); } else { node.normalize(); Node text = node.getFirstChild(); if (text != null) { retArray[i] = text.getNodeValue(); } } } } return retArray; }
From source file:com.adaptris.util.XmlUtils.java
/** * Method which updates the Text value of a specified Node * * @param value the new Text value/* ww w. j a v a 2 s . c o m*/ * @param n the node to be modified * @throws Exception on error. */ public void setNodeValue(String value, Node n) throws Exception { n.normalize(); n.getFirstChild().setNodeValue(value); }
From source file:com.zimbra.common.util.QuotedTextUtil.java
/** * Using the DOM structure of the message content, traverse node by node and * if we find a node that is recognized as a separator, remove all * subsequent elements/*www. j a v a 2s . com*/ * * @param text the message content * @return original content if the quoted content was found otherwise the * complete message content */ private String getOriginalHtmlContent(String text) { ArrayList<Node> nodeList = new ArrayList<Node>(); Node previousNode = null, sepNode = null; LineType previousType = null; boolean done = false; DOMParser parser = new DOMParser(); Document document; Node htmlNode = null; try { parser.parse(new InputSource(new StringReader(text))); document = parser.getDocument(); htmlNode = document.getFirstChild(); flatten(htmlNode, nodeList); for (int i = 0; i < nodeList.size(); i++) { Node currentNode = nodeList.get(i); if (currentNode.getNodeType() == ELEMENT_NODE) { currentNode.normalize(); } String nodeName = currentNode.getNodeName() != null ? currentNode.getNodeName() : ""; String nodeValue = currentNode.getNodeValue() != null ? currentNode.getNodeValue() : ""; LineType type = checkNode(currentNode); /* * Check for a multi-element "wrote:" attribution (usually a * combo of #text and A nodes), for example: * * On Feb 28, 2014, at 3:42 PM, Joe Smith <<a * href="mailto:jsmith@zimbra.com" * target="_blank">jsmith@zimbra.com</a>> wrote: * * If the current node is a #text with a date or "On ...", find * #text nodes within the next ten nodes, concatenate them, and * check the result. */ if (type == LineType.UNKNOWN && nodeName.equals("#text") && (MATCHER_ORIG_DATE.reset(nodeValue).matches() || MATCHER_ORIG_INTRO.reset(nodeValue).matches())) { String value = nodeValue; for (int j = 1; j < 10; j++) { Node tempNode = nodeList.get(i + j); if (tempNode != null && tempNode.getNodeName() != null && tempNode.getNodeName().equals("#text")) { value += tempNode.getNodeValue(); if ("/:$/".matches(value)) { type = getLineType(value.trim()); if (type == LineType.SEP_STRONG) { i = i + j; break; } } } } } if (type != null) { // TODO: confirm if you need to add the nodes in a map and // maintain count as done is javascript // definite separator if (type == LineType.SEP_STRONG || type == LineType.WROTE_STRONG) { sepNode = currentNode; done = true; break; } // some sort of line followed by a header if (type == LineType.HEADER && previousType == LineType.LINE) { sepNode = previousNode; done = true; break; } previousNode = currentNode; previousType = type; } } if (sepNode != null) { prune(sepNode, true); } if (done) { String originalText = getHtml(document); return (originalText == null || originalText.isEmpty()) ? text : originalText; } } catch (SAXException | IOException e) { ZimbraLog.soap.warn("Exception while removing quoted text from html message", e); } return text; }
From source file:org.structr.web.entity.dom.DOMNode.java
@Override public final void normalize() { Document document = getOwnerDocument(); if (document != null) { // merge adjacent text nodes until there is only one left Node child = getFirstChild(); while (child != null) { if (child instanceof Text) { Node next = child.getNextSibling(); if (next != null && next instanceof Text) { String text1 = child.getNodeValue(); String text2 = next.getNodeValue(); // create new text node Text newText = document.createTextNode(text1.concat(text2)); removeChild(child);//from w w w. ja v a2 s. c o m insertBefore(newText, next); removeChild(next); child = newText; } else { // advance to next node child = next; } } else { // advance to next node child = child.getNextSibling(); } } // recursively normalize child nodes if (hasChildNodes()) { Node currentChild = getFirstChild(); while (currentChild != null) { currentChild.normalize(); currentChild = currentChild.getNextSibling(); } } } }