List of usage examples for org.w3c.dom Node DOCUMENT_NODE
short DOCUMENT_NODE
To view the source code for org.w3c.dom Node DOCUMENT_NODE.
Click Source Link
Document
. From source file:Main.java
/** * @param node//w w w . j a va 2 s .c o m * @throws IOException */ public static void serializeNode(Node node) throws IOException { if (writer == null) writer = new BufferedWriter(new OutputStreamWriter(System.out)); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: Document doc = (Document) node; writer.write("<?xml version=\""); writer.write(doc.getXmlVersion()); writer.write("\" encoding=\"UTF-8\" standalone=\""); if (doc.getXmlStandalone()) writer.write("yes"); else writer.write("no"); writer.write("\"?>\n"); NodeList nodes = node.getChildNodes(); if (nodes != null) for (int i = 0; i < nodes.getLength(); i++) serializeNode(nodes.item(i)); break; case Node.ELEMENT_NODE: String name = node.getNodeName(); writer.write("<" + name); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); writer.write(" " + current.getNodeName() + "=\""); print(current.getNodeValue()); writer.write("\""); } writer.write(">"); NodeList children = node.getChildNodes(); if (children != null) { //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE)) // writer.write("\n"); for (int i = 0; i < children.getLength(); i++) serializeNode(children.item(i)); if ((children.item(0) != null) && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE)) writer.write(""); } writer.write("</" + name + ">"); break; case Node.TEXT_NODE: print(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: writer.write("CDATA"); print(node.getNodeValue()); writer.write(""); break; case Node.COMMENT_NODE: writer.write("<!-- " + node.getNodeValue() + " -->\n"); break; case Node.PROCESSING_INSTRUCTION_NODE: writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n"); break; case Node.ENTITY_REFERENCE_NODE: writer.write("&" + node.getNodeName() + ";"); break; case Node.DOCUMENT_TYPE_NODE: DocumentType docType = (DocumentType) node; String publicId = docType.getPublicId(); String systemId = docType.getSystemId(); String internalSubset = docType.getInternalSubset(); writer.write("<!DOCTYPE " + docType.getName()); if (publicId != null) writer.write(" PUBLIC \"" + publicId + "\" "); else writer.write(" SYSTEM "); writer.write("\"" + systemId + "\""); if (internalSubset != null) writer.write(" [" + internalSubset + "]"); writer.write(">\n"); break; } writer.flush(); }
From source file:Main.java
public static CDATASection addCDataText(Node node, String data) { Document doc = null;//from w w w.jav a 2 s . c o m if (node.getNodeType() == Node.DOCUMENT_NODE) { doc = (Document) node; } else { doc = node.getOwnerDocument(); } CDATASection e = doc.createCDATASection(data); node.appendChild(e); return e; }
From source file:Main.java
public static Document getDocument(Node node) { if (node.getNodeType() == Node.DOCUMENT_NODE) { return (Document) node; }/*from www.j a v a 2s. co m*/ return node.getOwnerDocument(); }
From source file:Main.java
/** Prints the specified node, recursively. * * @param node Node to be printed//from w ww .j a va 2s.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
protected static void print(PrintStream out, Node node) { if (node == null) return;/*from www . j a v a 2 s .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
@SuppressWarnings("fallthrough") private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean com) { if (rootNode == exclude) { return;/*from ww w . j a v a2s .co m*/ } switch (rootNode.getNodeType()) { case Node.ELEMENT_NODE: result.add(rootNode); Element el = (Element) rootNode; if (el.hasAttributes()) { NamedNodeMap nl = el.getAttributes(); for (int i = 0; i < nl.getLength(); i++) { result.add(nl.item(i)); } } //no return keep working case Node.DOCUMENT_NODE: for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) { if (r.getNodeType() == Node.TEXT_NODE) { result.add(r); while (r != null && r.getNodeType() == Node.TEXT_NODE) { r = r.getNextSibling(); } if (r == null) { return; } } getSetRec(r, result, exclude, com); } return; case Node.COMMENT_NODE: if (com) { result.add(rootNode); } return; case Node.DOCUMENT_TYPE_NODE: return; default: result.add(rootNode); } }
From source file:Main.java
/** * Returns absolute xpath for specified element, ignores namespaces * //from ww w .j a v a2 s .c o m * @param element * the element to create for * @return the elements path in its containing document */ public static String getElementPath(Element element) { Node elm = element; String result = elm.getNodeName() + "[" + getElementIndex(elm) + "]"; while (elm.getParentNode() != null && elm.getParentNode().getNodeType() != Node.DOCUMENT_NODE) { elm = elm.getParentNode(); result = elm.getNodeName() + "[" + getElementIndex(elm) + "]/" + result; } return "/" + result; }
From source file:Main.java
/** * Returns the node type name from the node type code * * @param nodeType/* w w w .j ava2 s . co m*/ * @return */ public static String getTypeName(short nodeType) { switch (nodeType) { case Node.ELEMENT_NODE: return "element"; case Node.DOCUMENT_NODE: return "document"; case Node.TEXT_NODE: return "text"; case Node.ATTRIBUTE_NODE: return "attribute"; case Node.CDATA_SECTION_NODE: return "cdata"; } return "Unknown[" + nodeType + "]"; }
From source file:Main.java
public static void stripDuplicateAttributes(Node node, Node parent) { // The output depends on the type of the node switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document doc = (Document) node; Node child = doc.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); }//from www. j a v a2s . c o m break; } case Node.ELEMENT_NODE: { Element elt = (Element) node; NamedNodeMap attrs = elt.getAttributes(); ArrayList nodesToRemove = new ArrayList(); int nodesToRemoveNum = 0; for (int i = 0; i < attrs.getLength(); i++) { final Node a = attrs.item(i); for (int j = 0; j < attrs.getLength(); j++) { final Node b = attrs.item(j); //if there are two attributes with same name if (i != j && (a.getNodeName().equals(b.getNodeName()))) { nodesToRemove.add(b); nodesToRemoveNum++; } } } for (int i = 0; i < nodesToRemoveNum; i++) { Attr nodeToDelete = (Attr) nodesToRemove.get(i); Element nodeToDeleteParent = (Element) node; // nodeToDelete.getParentNode(); nodeToDeleteParent.removeAttributeNode(nodeToDelete); } nodesToRemove.clear(); Node child = elt.getFirstChild(); while (child != null) { stripDuplicateAttributes(child, node); child = child.getNextSibling(); } break; } default: //do nothing break; } }
From source file:Main.java
public static String getTextContent(final Node node) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: case Node.ATTRIBUTE_NODE: case Node.ENTITY_NODE: case Node.ENTITY_REFERENCE_NODE: case Node.DOCUMENT_FRAGMENT_NODE: return mergeTextContent(node.getChildNodes()); case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.COMMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: return node.getNodeValue(); case Node.DOCUMENT_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.NOTATION_NODE: default://from w w w. j av a2s.c o m return null; } }