List of usage examples for org.w3c.dom Node getNodeValue
public String getNodeValue() throws DOMException;
From source file:Utils.java
public static String elementToString(Node n) { String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; }//from ww w. ja va 2 s . com if (name.startsWith("#")) { return ""; } StringBuffer sb = new StringBuffer(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); ; } else { sb.append("/>").append('\n'); } } else { sb.append('>').append('\n'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append(childToString); hasValidChildren = true; } } if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:Main.java
/** * Gets the value of an element. This method returns a concatenated String * from all its TEXT children./*from www . ja v a 2 s . c o m*/ * * @param element * a DOM tree element. * @return A String that contained in its TEXT children; or null if an error * occurred or the input contain non Node.TEXT_NODE node. */ public static String getElementString(Element element) { if (element == null) { return null; } StringBuffer sb = new StringBuffer(1000); NodeList nl = element.getChildNodes(); Node child = null; for (int i = 0, length = nl.getLength(); i < length; i++) { child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { sb.append(child.getNodeValue()); } else { return null; } } return sb.toString().trim(); }
From source file:Main.java
/**Recursive method which has a side effect of setting the result to the pretty-printed flat text version of an XML node. * @param finger the current node/*ww w. ja va 2 s . co m*/ * @param result the resultant string that is being built up. * @param indent the current indenting level. */ private static void doc2String(Node finger, StringBuffer result, int indent) { //Base Case for (int j = 0; j < indent; j++) result.append(' '); if (finger.getNodeType() == Node.TEXT_NODE) { result.append(finger.getNodeValue().trim()); result.append("\n"); return; } result.append('<'); //System.out.println("XMLUtils: appending " + finger.getNodeName() + " to output"); result.append(finger.getNodeName().trim()); result.append('>'); result.append("\n"); if (finger.hasChildNodes()) { NodeList childList = finger.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { doc2String(childList.item(i), result, indent + 1); } } for (int j = 0; j < indent; j++) result.append(' '); result.append("</"); //System.out.println("XMLUtils: appending end " + finger.getNodeName() + " to output"); result.append(finger.getNodeName().trim()); result.append('>'); result.append("\n"); }
From source file:Main.java
public static String elementToString(Node n) { String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; }//ww w.j a v a2 s .c o m if (name.startsWith("#")) { return ""; } StringBuilder sb = new StringBuilder(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { // if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) { if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); } else { sb.append("/>"); } } else { sb.append('>'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append('\n').append(childToString); hasValidChildren = true; } } if (hasValidChildren) { sb.append('\n'); } // if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) { if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); }
From source file:Main.java
/** * // w w w . j a va 2s . c om * @param node * @param classname * @return */ public static Object decode(Element node, String classname) { try { Class classObject = Class.forName(classname); Object object = classObject.newInstance(); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node child = attributes.item(i); String nodeName = child.getNodeName(); Field field = classObject.getField(nodeName); field.setAccessible(true); field.set(object, child.getNodeValue()); } return object; } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } catch (InstantiationException e) { e.printStackTrace(); return null; } catch (IllegalAccessException e) { e.printStackTrace(); return null; } catch (SecurityException e) { e.printStackTrace(); return null; } catch (NoSuchFieldException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static String getFullTextFromChildren(Element element) { if (element == null) { return null; }/*from w w w . j a v a 2 s. c o m*/ StringBuffer sb = new StringBuffer(1000); NodeList nl = element.getChildNodes(); Node child = null; int length = nl.getLength(); for (int i = 0; i < length; i++) { child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { sb.append(child.getNodeValue()); } } return sb.toString().trim(); }
From source file:DomUtils.java
/** * Extract the text value from the given DOM element, ignoring XML comments. * <p>Appends all CharacterData nodes and EntityReference nodes * into a single String value, excluding Comment nodes. * @see CharacterData/*from w w w . ja va 2s . c om*/ * @see EntityReference * @see Comment */ public static String getTextValue(Element valueEle) { StringBuffer value = new StringBuffer(); NodeList nl = valueEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node item = nl.item(i); if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) { value.append(item.getNodeValue()); } } return value.toString(); }
From source file:Main.java
public static int outputNode(Node outputNode, PrintWriter outputWriter, int curPos) { NodeList nodes = outputNode.getChildNodes(); int curNodeNum; if (outputNode.getNodeType() == Node.TEXT_NODE) { outputWriter.print(outputNode.getNodeValue()); } else {/*from w w w . j a v a2s. c o m*/ if (outputNode.getNodeName().equals("p")) { outputWriter.println(); } for (curNodeNum = 0; curNodeNum < nodes.getLength(); curNodeNum++) { Node curNode = nodes.item(curNodeNum); curPos = outputNode(curNode, outputWriter, curPos); //System.out.println(curNode.getNodeName()); //System.out.println(curNode.getNodeValue()); } } return (curPos); }
From source file:Main.java
/** * Gets the value of an element. This method returns a concatenated String * from all its TEXT children./*from w w w . j a va2s. co m*/ * * @param element * a DOM tree element. * @return A String that contained in its TEXT children; or null if an error * occurred. */ public static String getElementValue(Element element) { if (element == null) { return null; } StringBuffer sb = new StringBuffer(1000); NodeList nl = element.getChildNodes(); Node child = null; int length = nl.getLength(); for (int i = 0; i < length; i++) { child = nl.item(i); if (child.getNodeType() == Node.TEXT_NODE) { sb.append(child.getNodeValue()); } } return sb.toString().trim(); }
From source file:edu.duke.cabig.c3pr.webservice.integration.XMLUtils.java
/** * @param n1//from www .ja v a2 s . c o m * @param n2 * @return */ private static boolean isTextNodeEqual(Node n1, Node n2) { final String v1 = StringUtils.trim(normalize(n1.getNodeValue() + "")); final String v2 = StringUtils.trim(normalize(n2.getNodeValue() + "")); final boolean eq = StringUtils.equals(v1, v2); if (!eq) { log.info("Text nodes are not equal: " + v1 + " and " + v2); } return eq; }