To get all the textual content in the dom

@param document
@param individualTokens : default True : when set to true, each text node from dom is used to build the
text content : when set to false, the text content of whole is obtained at once.
@return
767384
public static String getTextContent(Document document, boolean individualTokens) {
        String textContent = null;
        if (individualTokens) {
            List<String> tokens = getTextTokens(document);
            textContent = StringUtils.join(tokens, ",");
        } else {
            textContent =
                    document.getDocumentElement().getTextContent().trim().replaceAll("\\s+", ",");
        }
        return textContent;
    }
657657
public static String convertXmlDocumentToString(DomDocument document) {
    StringWriter stringWriter = new StringWriter();
    StreamResult result = new StreamResult(stringWriter);
    transformDocumentToXml(document, result);
    return stringWriter.toString();
  }
700156
private Map<String, CharSequence> parseParams(Document dc) {
        Map<String, CharSequence> params = new HashMap<String, CharSequence>();
        NodeList paramsElements = dc.getElementsByTagNameNS(SCHEMA_LOCATION, TAG_PARAMS);
        if (paramsElements.getLength() > 0) {
            Element paramsElement = (Element) paramsElements.item(0);
            NodeList paramElements = paramsElement.getElementsByTagNameNS(SCHEMA_LOCATION, TAG_PARAM);
            for (int i = 0; i < paramElements.getLength(); i++) {
                Node paramElement = paramElements.item(i);
                String name = nodeAttribute(paramElement, TAG_PARAM_ATTR_NAME, "");
                String value = nodeAttribute(paramElement, TAG_PARAM_ATTR_VALUE, "");
                if (name != null && name.length() > 0) {
                    params.put(name, value);
                }
            }
        }
        return params;
    }
725887
public Document cloneDocument(DOMImplementation domImpl, Document document) {
    Element root = document.getDocumentElement();
    // New document
    Document result = domImpl.createDocument(root.getNamespaceURI(), root.getNodeName(), null);
    Element rroot = result.getDocumentElement();
    // Cloning the document element is a bit tricky.
    // This is adopted from DomUtilities#deepCloneDocument
    boolean before = true;
    for(Node n = document.getFirstChild(); n != null; n = n.getNextSibling()) {
      if(n == root) {
        before = false;
        copyAttributes(result, root, rroot);
        for(Node c = root.getFirstChild(); c != null; c = c.getNextSibling()) {
          final Node cl = cloneNode(result, c);
          if(cl != null) {
            rroot.appendChild(cl);
          }
        }
      }
      else {
        if(n.getNodeType() != Node.DOCUMENT_TYPE_NODE) {
          final Node cl = cloneNode(result, n);
          if(cl != null) {
            if(before) {
              result.insertBefore(cl, rroot);
            }
            else {
              result.appendChild(cl);
            }
          }
        }
      }
    }
    return result;
  }
772350
public void processDocument(BufferedReader document) throws IOException {
        int docIndex = documentCounter.getAndIncrement();

        Iterator<String> documentTokens = 
            IteratorFactory.tokenizeOrdered(document);

        // As we read in the document, generate a compressed version of it,
        // which we will use during the process space method to recompute all of
        // the word vectors' semantics
        ByteArrayOutputStream compressedDocument = 
            new ByteArrayOutputStream(4096);
        DataOutputStream dos = new DataOutputStream(compressedDocument);
        int tokens = 0; // count how many are in this document
        int unfilteredTokens = 0; // how many tokens remained after filtering

        IntegerVector docVector = createVector();
        docToVector.put(docIndex, docVector);

        while (documentTokens.hasNext()) {
            tokens++;
            String focusWord = documentTokens.next();

            // If we are filtering the semantic vectors, check whether this word
            // should have its semantics calculated.  In addition, if there is a
            // filter and it would have excluded the word, do not keep its
            // semantics around
            boolean calculateSemantics =
                semanticFilter.isEmpty() || semanticFilter.contains(focusWord)
                && !focusWord.equals(IteratorFactory.EMPTY_TOKEN);

	    // If the filter does not accept this word, skip the semantic
	    // processing, continue with the next word
            if (!calculateSemantics) {
                // Do not write out any removed tokens to save space
		continue;
	    }

            // Update the occurrences of this token
            unfilteredTokens++;
            add(docVector, getTermIndexVector(focusWord));

            // Update the compress version of the document with the token.
            //
            // NOTE: this call to termToIndex *must* come after the
            // getTermIndexVector() call, which is responsible for adding this
            // mapping if it doesn't already exist.
	    int focusIndex = termToIndex.get(focusWord);

            // write the term index into the compressed for the document for
            // later corpus reprocessing
            dos.writeInt(focusIndex);
        }

        document.close();
        
        dos.close();
        byte[] docAsBytes = compressedDocument.toByteArray();

        // Once the document is finished, write the compressed contents to the
        // corpus stream
        synchronized(compressedDocumentsWriter) {
            // Write how many terms were in this document after filtering
            compressedDocumentsWriter.writeInt(unfilteredTokens);
            compressedDocumentsWriter.write(docAsBytes, 0, docAsBytes.length);
        }
    }
552826
static final DocumentFragment createContent(Document doc, String text) {

        // [#150] Text might hold XML content, which can be leniently identified by the presence
        //        of either < or & characters (other entities, like >, ", ' are not stricly XML content)
        if (text != null && (text.contains("<") || text.contains("&"))) {
            DocumentBuilder builder = JOOX.builder();

            try {

                // [#128] Trimming will get rid of leading and trailing whitespace, which would
                // otherwise cause a HIERARCHY_REQUEST_ERR raised by the parser
                text = text.trim();

                // There is a processing instruction. We can safely assume
                // valid XML and parse it as such
                if (text.startsWith("<?xml")) {
                    Document parsed = builder.parse(new InputSource(new StringReader(text)));
                    DocumentFragment fragment = parsed.createDocumentFragment();
                    fragment.appendChild(parsed.getDocumentElement());

                    return (DocumentFragment) doc.importNode(fragment, true);
                }

                // Any XML document fragment. To be on the safe side, fragments
                // are wrapped in a dummy root node
                else {
                    String wrapped = "<dummy>" + text + "</dummy>";
                    Document parsed = builder.parse(new InputSource(new StringReader(wrapped)));
                    DocumentFragment fragment = parsed.createDocumentFragment();
                    NodeList children = parsed.getDocumentElement().getChildNodes();

                    // appendChild removes children also from NodeList!
                    while (children.getLength() > 0) {
                        fragment.appendChild(children.item(0));
                    }

                    return (DocumentFragment) doc.importNode(fragment, true);
                }
            }

            // This does not occur
            catch (IOException ignore) {}

            // The XML content is invalid
            catch (SAXException ignore) {}
        }

        // Plain text or invalid XML
        return null;
    }
635380
def parse_document(lexer: Lexer) -> DocumentNode:
    """"""
    start = lexer.token
    return DocumentNode(
        definitions=many_nodes(lexer, TokenKind.SOF, parse_definition, TokenKind.EOF),
        loc=loc(lexer, start),
    )
34336
final Document getPayloadDocument() throws ParserConfigurationException,
                                             IOException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(this, tc, "getPayloadDocument");

    Document domDoc = null;

    // If we already have a Document cached we don't need to build it again
    if (softRefToDocument != null) {
      domDoc = softRefToDocument.get();
    }

    // Otherwise we'll take the hit of building it, and cache it in a soft reference
    if (domDoc == null) {
      byte[] bytes = getBytes();
      if ((bytes != null) && (bytes.length > 0)) {
        DocumentBuilderFactory domFactory =DocumentBuilderFactory.newInstance();
        // We need to tell the factory that we want it to be namespace aware.
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(bytes);
        try {
          domDoc = builder.parse(is);
          softRefToDocument = new SoftReference<Document>(domDoc);
        }
        catch (SAXException e) {
          // No FFDC code needed
          // This is not really an error - it just means the data is not XML.
          // As the XPath Expression can't evaluate to True we just set the document to null;
          domDoc = null;
        }
      }
      else {
        domDoc = null;
      }
    }

    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(this, tc, "getPayloadDocument", domDoc);
    return domDoc;
  }
36004
private Document parse(final String data) {
    if (docBuilder == null || data == null) {
      return null;
    }

    Document document = null;

    try {
      // we change the system ID to a valid URI so that Crimson won't
      // complain. Indeed, "log4j.dtd" alone is not a valid URI which
      // causes Crimson to barf. The Log4jEntityResolver only cares
      // about the "log4j.dtd" ending.

      /**
       * resetting the length of the StringBuffer is dangerous, particularly
       * on some JDK 1.4 impls, there's a known Bug that causes a memory leak
       */
      StringBuffer buf = new StringBuffer(1024);

      if (!data.startsWith("<?xml")) {
        buf.append(BEGIN_PART);
      }

      buf.append(data);

      if (!data.endsWith(END_PART)) {
        buf.append(END_PART);
      }

      InputSource inputSource =
        new InputSource(new StringReader(buf.toString()));
      document = docBuilder.parse(inputSource);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return document;
  }
246781
Document createDocument(Entity entity) {
    int maxIndexingDepth = entity.getEntityType().getIndexingDepth();
    XContentBuilder contentBuilder;
    try {
      contentBuilder = XContentFactory.contentBuilder(JSON);
      XContentGenerator generator = contentBuilder.generator();
      generator.writeStartObject();
      createRec(entity, generator, 0, maxIndexingDepth);
      generator.writeEndObject();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    String documentId = toElasticsearchId(entity.getIdValue());
    return Document.create(documentId, contentBuilder);
  }
944831
public String getContent() {
		String content = "";
		
		NodeList list = dom.getChildNodes();
		for (int i=0;i<list.getLength();i++) {
			if (list.item(i) instanceof Text) {
				content += (list.item(i).getNodeValue());
			}
		}
		
		return content;
	}
278708
public static InputStream documentToPrettyInputStream(Node document) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        Source xmlSource = new DOMSource(document);
        Result outputTarget = new StreamResult(outputStream);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(xmlSource, outputTarget);

        InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
        return is;
    }
900929
def process_markdown_inside_elements(doc)
      elts = []
      @fragment.each_element('//*[@markdown]') do |e|
        elts << e
      end

      d = @fragment.children.first
      if d && HTML_INLINE_ELEMS.include?(first_node_name)
        elts << d unless d.attributes['markdown']
        elts += span_descendents(d)
      end

      # find span elements or elements with 'markdown' attribute
      elts.each do |e|
        # should we parse block-level or span-level?
        how = e.attributes['markdown']
        e.attributes.delete('markdown')

        next if "0" == how # user requests no markdown parsing inside
        parse_blocks = (how == 'block') || BLOCK_TAGS.include?(e.name)

        # Select all text children of e
        e.texts.each do |original_text|
          s = MaRuKu::Out::HTML.escapeHTML(original_text.value)
          unless s.strip.empty?
            # TODO extract common functionality
            parsed = parse_blocks ? doc.parse_text_as_markdown(s) : doc.parse_span(s)
            # restore leading and trailing spaces
            padding = /\A(\s*).*?(\s*)\z/.match(s)
            parsed = [padding[1]] + parsed + [padding[2]] if padding

            el = doc.md_el(:dummy, parsed)

            new_html = "<dummy>"
            el.children_to_html.each do |x|
              new_html << x.to_s
            end
            new_html << "</dummy>"

            newdoc = REXML::Document.new(new_html).root

            p = original_text.parent
            newdoc.children.each do |c|
              p.insert_before(original_text, c)
            end

            p.delete(original_text)
          end
        end
      end
    end
407419
public int getDocumentFrequency(Token token) 
    { 
        Integer df = (Integer)documentFrequency.get(token);
        if (df == null) return 0;
        else return df.intValue();
    }
658478
public static Element[] toElementArray(Document doc, Object o) throws PageException {
	// Node[]
	if (o instanceof Node[]) {
	    Node[] nodes = (Node[]) o;
	    if (_isAllOfSameType(nodes, Node.ELEMENT_NODE)) return (Element[]) nodes;

	    Element[] elements = new Element[nodes.length];
	    for (int i = 0; i < nodes.length; i++) {
		elements[i] = toElement(doc, nodes[i]);
	    }
	    return elements;
	}
	// Collection
	else if (o instanceof Collection) {
	    Collection coll = (Collection) o;
	    Iterator<Object> it = coll.valueIterator();
	    List<Element> elements = new ArrayList<Element>();
	    while (it.hasNext()) {
		elements.add(toElement(doc, it.next()));
	    }
	    return elements.toArray(new Element[elements.size()]);
	}
	// Node Map and List
	Node[] nodes = _toNodeArray(doc, o);
	if (nodes != null) return toElementArray(doc, nodes);
	// Single Text Node
	try {
	    return new Element[] { toElement(doc, o) };
	}
	catch (ExpressionException e) {
	    throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Element Array");
	}
    }
463871
@Programmatic
    public List<Paperclip> findByDocument(final DocumentAbstract document) {
        return repositoryService.allMatches(
                new QueryDefault<>(Paperclip.class,
                        "findByDocument",
                        "document", document));
    }
239481
public static Document toDocument(String xml) {
		try {

			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
					.newInstance();
			documentBuilderFactory.setNamespaceAware(true);
			documentBuilderFactory.setIgnoringElementContentWhitespace(true);
			documentBuilderFactory.isIgnoringComments();
			documentBuilderFactory.setCoalescing(true);

			DocumentBuilder documentBuilder = documentBuilderFactory
					.newDocumentBuilder();

			InputSource inputSource = new InputSource();

			inputSource.setCharacterStream(new StringReader(xml));

			Document document = documentBuilder.parse(inputSource);

			removeUnusedNamespaces(document);

			return document;

		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		} catch (SAXException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}