NodeList itemCheckedNodeList = positionElement.getElementsByTagName("remark"); Element itemCheckedElement = (Element) itemCheckedNodeList.item(0); NodeList itemCheckedLN = itemCheckedElement.getChildNodes(); Text itemCheckedText = (Text) itemCheckedLN.item(0); if (itemCheckedText == null) { Document doc = itemCheckedElement.getOwnerDocument(); itemCheckedText = doc.createTextNode("remark"); itemCheckedElement.appendChild(itemCheckedText); } itemCheckedText.setTextContent("Here is a new comment");
InputSource stream = new InputSource(inputStream); XPath xpath = XPathFactory.newInstance().newXPath(); Document doc = (Document) xpath.evaluate("/", stream, XPathConstants.NODE); NodeList nodes = (NodeList) xpath.evaluate("//text()", doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Text text = (Text) nodes.item(i); text.setTextContent(text.getTextContent().replaceAll("\\s{2,}", " ")); } // check results TransformerFactory.newInstance() .newTransformer() .transform(new DOMSource(doc), new StreamResult(System.out));
static void trimNode(Node node, List<org.w3c.dom.Text> nodes) { if (node.getNodeType() == Node.TEXT_NODE) { node.normalize(); org.w3c.dom.Text txt = (org.w3c.dom.Text) node; txt.setTextContent(txt.getTextContent().trim()); nodes.add(txt); } else { NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { trimNode(nl.item(i), nodes); } } }
static void trimNode(Node node, List<org.w3c.dom.Text> nodes) { if (node.getNodeType() == Node.TEXT_NODE) { node.normalize(); org.w3c.dom.Text txt = (org.w3c.dom.Text) node; txt.setTextContent(txt.getTextContent().trim()); nodes.add(txt); } else { NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { trimNode(nl.item(i), nodes); } } }
/** * Runs the test case. * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; NodeList elemList; Element elem; Text txt; String textContent; doc = (Document) load("hc_staff", true); elemList = doc.getElementsByTagName("em"); elem = (Element) elemList.item(0); txt = (Text) elem.getFirstChild(); txt.setTextContent("Text"); textContent = txt.getTextContent(); assertEquals("nodegettextcontent10", "Text", textContent); } /**
/** * Runs the test case. * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; DOMConfiguration domConfig; NodeList itemList; Element elementStrong; Text textNode; boolean canSet; boolean hasWhitespace; doc = (Document) load("hc_staff", false); domConfig = doc.getDomConfig(); canSet = domConfig.canSetParameter("element-content-whitespace", Boolean.TRUE); assertTrue("domconfigurationcansetparameter06_1", canSet); itemList = doc.getElementsByTagNameNS("*", "strong"); elementStrong = (Element) itemList.item(0); textNode = (Text) elementStrong.getFirstChild(); textNode.setTextContent(" "); hasWhitespace = textNode.isElementContentWhitespace(); assertTrue("domconfigurationsetparameter06_2", hasWhitespace); doc.normalizeDocument(); itemList = doc.getElementsByTagNameNS("*", "strong"); elementStrong = (Element) itemList.item(0); textNode = (Text) elementStrong.getFirstChild(); hasWhitespace = textNode.isElementContentWhitespace(); assertTrue("domconfigurationsetparameter06_3", hasWhitespace); } /**
private void applyText( org.w3c.dom.Text textNode, Object ctx ){ if( ctx instanceof ApplyText ){ ((ApplyText)ctx).applyText(textNode); if( ctx instanceof ApplyContinue ){ if( !((ApplyContinue)ctx).isContinue() ){ return; } }else{ return; } } String txt = textNode.getTextContent(); if( txt==null || txt.length()==0 )return; // TODO cache text,ctx -> BasicTemplate.Context BasicTemplate tmpl = getBasicTemplate(txt); BasicTemplate.ContextBuilder ctxb = tmpl.context(ctx.getClass()); if( isUseJavaScript() ) ctxb.useJavaScript(); BasicTemplate.Context ctxTemplate = ctxb.build(); String res = ctxTemplate.eval(ctx); if( res!=null )textNode.setTextContent(res); }