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
/** * Serializes the given XML tree to string form, including the standard * XML header and indentation if desired. This method relies on the * serialization API from Apache Xerces, since JAXP has on equivalent. * /*w w w .j a v a 2 s.c o m*/ * @param xml * The XML tree to serialize. * @param printHeader * True if you want the XML header printed before the XML. * @param printIndents * True if you want pretty-printing - child elements will be * indented with symmetry. * @return The string representation of the given Node. */ public static String toString(final Node xml, final boolean printHeader, final boolean printIndents) { final short type = xml.getNodeType(); if (type == Node.TEXT_NODE) return xml.getNodeValue(); // // NOTE: This serialization code is not part of JAXP/DOM - it is // specific to Xerces and creates a Xerces dependency for // this class. // final XMLSerializer serializer = new XMLSerializer(); serializer.setNamespaces(true); final OutputFormat formatter = new OutputFormat(); formatter.setOmitXMLDeclaration(!printHeader); formatter.setIndenting(printIndents); serializer.setOutputFormat(formatter); final StringWriter writer = new StringWriter(); serializer.setOutputCharStream(writer); try { if (type == Node.DOCUMENT_NODE) serializer.serialize((Document) xml); else serializer.serialize((Element) xml); } // // we are using a StringWriter, so this "should never happen". the // StringWriter implementation writes to a StringBuffer, so there's // no file I/O that could fail. // // if it DOES fail, we re-throw with a more serious error, because // this a very common operation. // catch (final IOException error) { throw new RuntimeException(error.getMessage(), error); } return writer.toString(); }
From source file:de.betterform.xml.xforms.ui.AbstractUIElement.java
protected String getTargetReference(Element node, String targetRef) { if (node == null) return targetRef; if (!(node instanceof Element)) return targetRef; Object xfObject = ((Element) node).getUserData(""); if (xfObject != null && xfObject instanceof RepeatItem) { int position = ((RepeatItem) xfObject).getPosition(); targetRef = "," + position + "]/" + targetRef; } else if (xfObject != null && xfObject instanceof Repeat) { int position = ((Repeat) xfObject).getPosition(); targetRef = "[" + position + "" + targetRef; }/* w w w . j ava2 s .co m*/ Node parent = node.getParentNode(); if (parent.getNodeType() == Node.DOCUMENT_NODE || parent.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE || parent == null) { return "/" + targetRef; } else if (parent.getNodeType() == Node.ELEMENT_NODE) { return getTargetReference((Element) parent, targetRef); } else { LOGGER.warn("Unkown type: " + parent); return targetRef; } }
From source file:fr.gouv.finances.dgfip.xemelios.utils.TextWriter.java
private void writeNode(Writer writer, Node node) throws IOException { short type = node.getNodeType(); switch (type) { case Node.DOCUMENT_NODE: document(writer, (Document) node); break;/*from w ww . ja va 2 s . c o m*/ case Node.DOCUMENT_FRAGMENT_NODE: documentFragment(writer, (DocumentFragment) node); break; case Node.DOCUMENT_TYPE_NODE: documentType(writer, (DocumentType) node); break; case Node.ELEMENT_NODE: element(writer, (Element) node); break; case Node.ATTRIBUTE_NODE: attribute(writer, (Attr) node); break; case Node.ENTITY_REFERENCE_NODE: entityReference(writer, (EntityReference) node); break; case Node.ENTITY_NODE: entity(writer, (Entity) node); break; case Node.NOTATION_NODE: notation(writer, (Notation) node); break; case Node.PROCESSING_INSTRUCTION_NODE: procInst(writer, (ProcessingInstruction) node); break; case Node.TEXT_NODE: text(writer, (Text) node); break; case Node.CDATA_SECTION_NODE: cDataSection(writer, (CDATASection) node); break; case Node.COMMENT_NODE: comment(writer, (Comment) node); break; } }
From source file:com.duroty.lucene.parser.SimpleXmlParser.java
/** * DOCUMENT ME!/*from w ww . ja v a2 s .c o m*/ * * @param node DOCUMENT ME! */ private void traverseTree(Node node) { /*if (sleep > 0) { try { Thread.sleep(sleep); } catch (Exception ex) { } }*/ if (node == null) { return; } int type = node.getNodeType(); if (type == Node.DOCUMENT_NODE) { traverseTree(((org.w3c.dom.Document) node).getDocumentElement()); } else if (type == Node.TEXT_NODE) { try { String value = node.getNodeValue(); if ((value != null) && !value.equals("") && !value.startsWith("\n")) { buffer.append(value + "\n"); } } catch (Exception ex) { //buffer.append("\n"); } NodeList childNodes = node.getChildNodes(); if (childNodes != null) { for (int i = 0; i < childNodes.getLength(); i++) { traverseTree(childNodes.item(i)); } } } else { NodeList childNodes = node.getChildNodes(); if (childNodes != null) { for (int i = 0; i < childNodes.getLength(); i++) { traverseTree(childNodes.item(i)); } } } }
From source file:org.kuali.coeus.s2sgen.impl.validate.S2SValidatorServiceImpl.java
/** * // w w w .j a v a 2 s . co m * This method receives a node, fetches its name, and recurses up to its parent node, until it reaches the document node, thus * creating the XPath of the node passed and returns it as String * * @param node for which Document node has to found. * @return String which represents XPath of the node */ protected String getXPath(Node node) { if (node == null || node.getNodeType() == Node.DOCUMENT_NODE) { return ""; } else { return getXPath(node.getParentNode()) + "/" + node.getNodeName(); } }
From source file:gov.nih.nci.cagrid.opensaml.SAMLObject.java
/** * Installs the root node of this DOM as the document element * * @return The root node so planted//from w ww . ja va 2s .c om */ protected Node plantRoot() { if (root != null) { Node domroot = root; while (domroot.getParentNode() != null && domroot.getParentNode().getNodeType() != Node.DOCUMENT_NODE) domroot = domroot.getParentNode(); Element e = root.getOwnerDocument().getDocumentElement(); if (e != null && e != domroot) root.getOwnerDocument().replaceChild(domroot, e); else if (e == null) root.getOwnerDocument().appendChild(domroot); } return root; }
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 w ww .j ava 2 s. c o m * @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:cc.siara.csv_ml.ParsedObject.java
/** * Adds new W3C Element object and makes it current * // w w w. j a va 2 s . c o m * @param node_name * Name of new element to be created */ private void addNewElement(String node_name) { Element new_node = null; if (cur_element.getNodeType() == Node.DOCUMENT_NODE) { // If given input tries to add more than one root, throw error ex.set_err(MultiLevelCSVParser.E_ONLY_ONE_ROOT); return; } else { // If directive specifies the first element as root, // do not add new. Otherwise add new Element if (!cur_element.equals(doc.getDocumentElement()) || !node_name.equals(csv_ml_root)) { int cIdx = node_name.indexOf(':'); if (cIdx != -1) { currentElementNS = node_name.substring(0, cIdx); node_name = node_name.substring(cIdx + 1); } else currentElementNS = ""; new_node = doc.createElement(node_name); cur_element.appendChild(new_node); } } if (new_node != null) { last_element = new_node; cur_element = last_element; } }
From source file:com.gargoylesoftware.htmlunit.html.HTMLParser.java
/** * Parses the HTML content from the given string into an object tree representation. * * @param parent where the new parsed nodes will be added to * @param context the context to build the fragment context stack * @param source the (X)HTML to be parsed * @throws SAXException if a SAX error occurs * @throws IOException if an IO error occurs *//*from w ww . j a v a2s. co m*/ public static void parseFragment(final DomNode parent, final DomNode context, final String source) throws SAXException, IOException { final HtmlPage page = (HtmlPage) parent.getPage(); final URL url = page.getUrl(); final HtmlUnitDOMBuilder domBuilder = new HtmlUnitDOMBuilder(parent, url, source); domBuilder.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true); // build fragment context stack DomNode node = context; final List<QName> ancestors = new ArrayList<>(); while (node != null && node.getNodeType() != Node.DOCUMENT_NODE) { ancestors.add(0, new QName(null, node.getNodeName(), null, null)); node = node.getParentNode(); } if (ancestors.isEmpty() || !"html".equals(ancestors.get(0).localpart)) { ancestors.add(0, new QName(null, "html", null, null)); } if (ancestors.size() == 1 || !"body".equals(ancestors.get(1).localpart)) { ancestors.add(1, new QName(null, "body", null, null)); } domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_TAGS, true); domBuilder.setProperty(HTMLTagBalancer.FRAGMENT_CONTEXT_STACK, ancestors.toArray(new QName[] {})); final XMLInputSource in = new XMLInputSource(null, url.toString(), null, new StringReader(source), null); page.registerParsingStart(); page.registerSnippetParsingStart(); try { domBuilder.parse(in); } finally { page.registerParsingEnd(); page.registerSnippetParsingEnd(); } }
From source file:com.cellngine.util.FXMLValidator.java
private void checkNode(final Node node) throws InvalidFXMLException { final String nodeName = node.getNodeName(); final short nodeType = node.getNodeType(); if (nodeType == Node.ELEMENT_NODE) { if (!ALLOWED_ELEMENTS.isElementAllowed(nodeName)) { throw new InvalidFXMLException("Element type \"" + nodeName + "\" not allowed"); }// w w w .j a v a2s . c o m final NamedNodeMap nodeAttributes = node.getAttributes(); for (int i = 0; i < nodeAttributes.getLength(); i++) { checkAttributeNode(nodeAttributes.item(i), nodeName); } } else if (nodeType == Node.TEXT_NODE || nodeType == Node.DOCUMENT_NODE) { } else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE && node.getNodeName().equals("import")) { if (!ALLOWED_IMPORTS.contains(node.getNodeValue())) { throw new InvalidFXMLException("Import \"" + node.getNodeValue() + "\" not allowed."); } } else if (nodeType != Node.COMMENT_NODE) { throw new InvalidFXMLException("Unrecognized node: type: \"" + nodeType + "\", name: \"" + node.getNodeName() + "\", value: \"" + node.getNodeValue() + "\""); } final NodeList nodeChildren = node.getChildNodes(); for (int i = 0; i < nodeChildren.getLength(); i++) { checkNode(nodeChildren.item(i)); } }