List of usage examples for org.w3c.dom Attr getNodeValue
public String getNodeValue() throws DOMException;
From source file:Main.java
/** * Print SAML Attribute Element and replace its prefix with the input * prefix.//from ww w .jav a 2 s . c om * * @param node * A DOM tree Node * @param prefix * A String representing the new prefix * @return An xml String representation of the DOM tree. */ public static String printAttributeValue(Element node, String prefix) { if (node == null) { return null; } StringBuffer xml = new StringBuffer(100); xml.append('<'); xml.append(prefix).append(node.getLocalName()); NamedNodeMap attrs = node.getAttributes(); int length = attrs.getLength(); for (int i = 0; i < length; i++) { Attr attr = (Attr) attrs.item(i); xml.append(' '); xml.append(attr.getNodeName()); xml.append("=\""); // xml.append(normalize(attr.getNodeValue())); xml.append(attr.getNodeValue()); xml.append('"'); } xml.append('>'); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { xml.append(print(children.item(i))); } } xml.append("</"); xml.append(prefix).append(node.getLocalName()); xml.append('>'); return xml.toString(); }
From source file:Main.java
/** Prints the specified node, recursively. * * @param node Node to be printed//from ww w. ja va2s. c o m */ public static void print(Node node) { // is there anything to do? if (node == null) { return; } System.out.println(""); int type = node.getNodeType(); switch (type) { // print document case Node.DOCUMENT_NODE: { /* if (!canonical) { if (Encoding.equalsIgnoreCase("DEFAULT")) Encoding = "UTF-8"; else if(Encoding.equalsIgnoreCase("Unicode")) Encoding = "UTF-16"; else Encoding = MIME2Java.reverse(Encoding); out.println("<?xml version=\"1.0\" encoding=\"" + Encoding + "\"?>"); } */ print(((Document) node).getDocumentElement()); out.flush(); break; } // print element with attributes case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); Attr attrs[] = sortAttributes(node.getAttributes()); for (int i = 0; i < attrs.length; i++) { Attr attr = attrs[i]; out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } out.print('>'); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { if (canonical) { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { print(children.item(i)); } } } else { out.print('&'); out.print(node.getNodeName()); out.print(';'); } break; } // print cdata sections case Node.CDATA_SECTION_NODE: { if (canonical) { out.print(normalize(node.getNodeValue())); } else { out.print("<![CDATA["); out.print(node.getNodeValue()); out.print("]]>"); } break; } // print DocumentType sections case Node.DOCUMENT_TYPE_NODE: { out.print("<!DOCTYPE "); out.print(((DocumentType) node).getName()); out.print(" SYSTEM "); out.print(((DocumentType) node).getSystemId()); out.print(">"); break; } // print text case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String data = node.getNodeValue(); if (data != null && data.length() > 0) { out.print(' '); out.print(data); } out.print("?>"); break; } } if (type == Node.ELEMENT_NODE) { out.print("</"); out.print(node.getNodeName()); out.print('>'); } out.flush(); }
From source file:Main.java
/** * Print a Node tree recursively./*from ww w . ja va 2s . co m*/ * @param node A DOM tree Node * @return An xml String representation of the DOM tree. */ public static String print(Node node) { if (node == null) { return null; } StringBuffer xml = new StringBuffer(100); int type = node.getNodeType(); switch (type) { // print element with attributes case Node.ELEMENT_NODE: { xml.append('<'); xml.append(node.getNodeName()); NamedNodeMap attrs = node.getAttributes(); int length = attrs.getLength(); ; for (int i = 0; i < length; i++) { Attr attr = (Attr) attrs.item(i); xml.append(' '); xml.append(attr.getNodeName()); xml.append("=\""); //xml.append(normalize(attr.getNodeValue())); xml.append(attr.getNodeValue()); xml.append('"'); } xml.append('>'); NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { xml.append(print(children.item(i))); } } break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { NodeList children = node.getChildNodes(); if (children != null) { int len = children.getLength(); for (int i = 0; i < len; i++) { xml.append(print(children.item(i))); } } break; } // print cdata sections case Node.CDATA_SECTION_NODE: { xml.append("<![CDATA["); xml.append(node.getNodeValue()); xml.append("]]>"); break; } // print text case Node.TEXT_NODE: { //xml.append(normalize(node.getNodeValue())); xml.append(node.getNodeValue()); break; } // print processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { xml.append("<?"); xml.append(node.getNodeName()); String data = node.getNodeValue(); if ((data != null) && (data.length() > 0)) { xml.append(' '); xml.append(data); } xml.append("?>"); break; } } if (type == Node.ELEMENT_NODE) { xml.append("</"); xml.append(node.getNodeName()); xml.append('>'); } return xml.toString(); }
From source file:Main.java
protected static void print(PrintStream out, Node node) { if (node == null) return;//w ww .j av a2s .co m short type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: { out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); NodeList nodelist = node.getChildNodes(); int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType docType = (DocumentType) node; out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n"); break; } case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); NamedNodeMap map = node.getAttributes(); if (map != null) { int size = map.getLength(); for (int i = 0; i < size; i++) { Attr attr = (Attr) map.item(i); out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } } if (!node.hasChildNodes()) out.print("/>"); else { out.print('>'); NodeList nodelist = node.getChildNodes(); int numChildren = nodelist.getLength(); for (int i = 0; i < numChildren; i++) print(out, nodelist.item(i)); out.print("</"); out.print(node.getNodeName()); out.print('>'); } break; } case Node.ENTITY_REFERENCE_NODE: { NodeList nodelist = node.getChildNodes(); if (nodelist != null) { int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); } break; } case Node.CDATA_SECTION_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String s = node.getNodeValue(); if (s != null && s.length() > 0) { out.print(' '); out.print(s); } out.print("?>"); break; } case Node.COMMENT_NODE: { out.print("<!--"); out.print(node.getNodeValue()); out.print("-->"); break; } default: { out.print(normalize(node.getNodeValue())); break; } } out.flush(); }
From source file:Main.java
protected static void print(PrintStream out, Node node) { if (node == null) return;//from w w w . jav a 2s . c o m short type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: { out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); NodeList nodelist = node.getChildNodes(); int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType docType = (DocumentType) node; out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n"); break; } case Node.ELEMENT_NODE: { out.print('<'); out.print(node.getNodeName()); NamedNodeMap map = node.getAttributes(); if (map != null) { int size = map.getLength(); for (int i = 0; i < size; i++) { Attr attr = (Attr) map.item(i); out.print(' '); out.print(attr.getNodeName()); out.print("=\""); out.print(normalize(attr.getNodeValue())); out.print('"'); } } if (!node.hasChildNodes()) out.print("/>"); else { out.print('>'); NodeList nodelist = node.getChildNodes(); int numChildren = nodelist.getLength(); for (int i = 0; i < numChildren; i++) print(out, nodelist.item(i)); out.print("</"); out.print(node.getNodeName()); out.print('>'); } break; } case Node.ENTITY_REFERENCE_NODE: { NodeList nodelist = node.getChildNodes(); if (nodelist != null) { int size = nodelist.getLength(); for (int i = 0; i < size; i++) print(out, nodelist.item(i)); } break; } case Node.CDATA_SECTION_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.TEXT_NODE: { out.print(normalize(node.getNodeValue())); break; } case Node.PROCESSING_INSTRUCTION_NODE: { out.print("<?"); out.print(node.getNodeName()); String s = node.getNodeValue(); if (s != null && s.length() > 0) { out.print(' '); out.print(s); } out.print("?>"); break; } case Node.COMMENT_NODE: { out.print("<!--"); out.print(node.getNodeValue()); out.print("-->"); break; } default: { out.print(normalize(node.getNodeValue())); break; } } out.flush(); }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /*from w ww . j av a2s.c o m*/ * @param out The <CODE>PrintStream</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(Node) * @see #dump(PrintStream, Node) * @since TFP 1.0 */ private static void doDump(PrintStream out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * //from www . ja v a 2 s . c om * @param out The <CODE>PrintWriter</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(PrintWriter, Node) * @since TFP 1.0 */ private static void doDump(PrintWriter out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:Main.java
/** * _more_/*from w w w . j a v a 2 s . c om*/ * * @param html _more_ * @param node _more_ */ public static void toHtml(StringBuffer html, Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: { NodeList children = node.getChildNodes(); int numChildren = children.getLength(); html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>"); html.append(": "); for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { String v = child.getNodeValue(); if (v == null) { continue; } if (v.trim().length() == 0) { continue; } html.append(v); html.append(" "); } } boolean didone = false; NamedNodeMap nnm = node.getAttributes(); if (nnm != null) { for (int i = 0; i < nnm.getLength(); i++) { Attr attr = (Attr) nnm.item(i); String attrName = attr.getNodeName(); if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) { continue; } if (!didone) { html.append("<ul>"); didone = true; } html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue()); html.append("<br>\n"); } } int cnt = 0; for (int i = 0; i < numChildren; i++) { Node child = children.item(i); if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) { continue; } if (!didone) { html.append("<ul>"); didone = true; } if (cnt > 0) { html.append("<br>"); } toHtml(html, child); cnt++; } if (didone) { html.append("</ul>"); } break; } } }
From source file:DOMTreeFull.java
public static String toString(Node node) { StringBuffer sb = new StringBuffer(); // is there anything to do? if (node == null) { return ""; }/* ww w .j a va 2 s . c om*/ int type = node.getNodeType(); sb.append(whatArray[type]); sb.append(" : "); sb.append(node.getNodeName()); String value = node.getNodeValue(); if (value != null) { sb.append(" Value: \""); sb.append(value); sb.append("\""); } switch (type) { // document case Node.DOCUMENT_NODE: { break; } // element with attributes case Node.ELEMENT_NODE: { Attr attrs[] = sortAttributes(node.getAttributes()); if (attrs.length > 0) sb.append(" ATTRS:"); for (int i = 0; i < attrs.length; i++) { Attr attr = attrs[i]; sb.append(' '); sb.append(attr.getNodeName()); sb.append("=\""); sb.append(normalize(attr.getNodeValue())); sb.append('"'); } sb.append('>'); break; } // handle entity reference nodes case Node.ENTITY_REFERENCE_NODE: { break; } // cdata sections case Node.CDATA_SECTION_NODE: { break; } // text case Node.TEXT_NODE: { break; } // processing instruction case Node.PROCESSING_INSTRUCTION_NODE: { break; } // comment node case Node.COMMENT_NODE: { break; } // DOCTYPE node case Node.DOCUMENT_TYPE_NODE: { break; } // Notation node case Node.NOTATION_NODE: { sb.append("public:"); String id = ((Notation) node).getPublicId(); if (id == null) { sb.append("PUBLIC "); sb.append(id); sb.append(" "); } id = ((Notation) node).getSystemId(); if (id == null) { sb.append("system: "); sb.append(id); sb.append(" "); } break; } } return sb.toString(); }
From source file:com.gargoylesoftware.htmlunit.xml.XmlUtil.java
private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML) { if (source.getNodeType() == Node.TEXT_NODE) { return new DomText(page, source.getNodeValue()); }/*from w w w. j a v a 2 s . co m*/ if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue()); } if (source.getNodeType() == Node.COMMENT_NODE) { return new DomComment(page, source.getNodeValue()); } if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) { final DocumentType documentType = (DocumentType) source; return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId()); } final String ns = source.getNamespaceURI(); String localName = source.getLocalName(); if (handleXHTMLAsHTML && HTMLParser.XHTML_NAMESPACE.equals(ns)) { final ElementFactory factory = HTMLParser.getFactory(localName); return factory.createElementNS(page, ns, localName, namedNodeMapToSaxAttributes(source.getAttributes())); } final NamedNodeMap nodeAttributes = source.getAttributes(); if (page != null && page.isHtmlPage()) { localName = localName.toUpperCase(Locale.ROOT); } final String qualifiedName; if (source.getPrefix() == null) { qualifiedName = localName; } else { qualifiedName = source.getPrefix() + ':' + localName; } final String namespaceURI = source.getNamespaceURI(); if (HTMLParser.SVG_NAMESPACE.equals(namespaceURI)) { return HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName, namedNodeMapToSaxAttributes(nodeAttributes)); } final Map<String, DomAttr> attributes = new LinkedHashMap<>(); for (int i = 0; i < nodeAttributes.getLength(); i++) { final Attr attribute = (Attr) nodeAttributes.item(i); final String attributeNamespaceURI = attribute.getNamespaceURI(); final String attributeQualifiedName; if (attribute.getPrefix() != null) { attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName(); } else { attributeQualifiedName = attribute.getLocalName(); } final String value = attribute.getNodeValue(); final boolean specified = attribute.getSpecified(); final DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified); attributes.put(attribute.getNodeName(), xmlAttribute); } return new DomElement(namespaceURI, qualifiedName, page, attributes); }