private String getText(Element element) { StringBuilder sb = new StringBuilder(); NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); ++i) { Node child = nodes.item(i); switch (child.getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: CharacterData cdata = (CharacterData) child; sb.append(cdata.getData()); break; case Node.ENTITY_REFERENCE_NODE: EntityReference ref = (EntityReference) child; sb.append(ref.getNodeValue()); break; } } return sb.toString(); } }
static private void simplify(Node parent, StringBuilder collectorTextChildBuff) { Node collectorTextChild = null; Node child = parent.getFirstChild(); while (child != null) { Node next = child.getNextSibling(); if (child.hasChildNodes()) { if (collectorTextChild != null) { ((CharacterData) collectorTextChild).setData(collectorTextChildBuff.toString()); collectorTextChildBuff.setLength(0); ((CharacterData) collectorTextChild).setData(collectorTextChildBuff.toString()); collectorTextChildBuff.setLength(0); ((CharacterData) collectorTextChild).setData(collectorTextChildBuff.toString()); collectorTextChildBuff.setLength(0);
doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("configuration").item(0).getChildNodes(); for (int ix = 0; ix < nodeList.getLength(); ix++) { Node node = nodeList.item(ix); Element el = node.getNodeType() == Node.ELEMENT_NODE ? (Element) node : null; value = el.getTextContent(); value = ((CharacterData) el).getData(); configurationMap.put(el.getNodeName(), value);
/** * @param node a node. * @return the text content of the <code>node</code>. */ private static String getTextContent(Node node) { StringBuffer content = new StringBuffer(); NodeList nodes = node.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n.getNodeType() == Node.TEXT_NODE) { content.append(((CharacterData) n).getData()); } } return content.toString(); }
Element root = doc.getDocumentElement(); int rcode = new Integer(((CharacterData) (root.getElementsByTagName("rcode").item(0).getFirstChild())).getData().trim()).intValue(); String message = ((CharacterData) (root.getElementsByTagName("rmessage").item(0).getFirstChild())).getData().trim(); (root.getElementsByTagName("sessionid").item(0).getFirstChild())).getData().trim();
static private String getText(Node node) { String result = ""; if (node instanceof Text || node instanceof CDATASection) { result = ((org.w3c.dom.CharacterData) node).getData(); } else if (node instanceof Element) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { result += getText(children.item(i)); } } else if (node instanceof Document) { result = getText(((Document) node).getDocumentElement()); } return result; }
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public XmlElement(final Element element) { this.name = element.getNodeName(); final NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { final Node item = attributes.item(i); this.attributes.put(item.getNodeName(), item.getNodeValue()); } final StringBuilder textValue = new StringBuilder(); final NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { final Node node = children.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { this.children.add(new XmlElement((Element) node)); } if (node.getNodeType() == Node.TEXT_NODE) { textValue.append(node.getNodeValue()); } if (node.getNodeType() == Node.CDATA_SECTION_NODE) { textValue.append(((CharacterData) node).getData()); } } this.value = textValue.toString(); }
/** * Runs the test case. * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; NodeList elementList; Node nameNode; CharacterData child; String childData; doc = (Document) load("hc_staff", true); elementList = doc.getElementsByTagName("acronym"); nameNode = elementList.item(0); child = (CharacterData) nameNode.getFirstChild(); child.deleteData(0, 16); childData = child.getData(); assertEquals("data", "Dallas, Texas 98551", childData); } /**
Document doc = builder.parse(file); NodeList nodes = doc.getElementsByTagName("topic"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NodeList title = element.getElementsByTagName("title"); Element line = (Element) title.item(0); System.out.println("Title: " + getCharacterDataFromElement(line)); Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData();
while (items.hasNext()) { Document valdoc = (Document) items.next(); NodeList list = valdoc.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node newNode = element.getOwnerDocument().importNode(list.item(i), true); element.appendChild(newNode); Node lastChild = valdoc.getLastChild(); NodeList list = lastChild.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node newNode = element.getOwnerDocument().importNode(list.item(i), true); element.appendChild(newNode); data = text; data.setData(String.valueOf(value));
NodeList nl = myDoc.getElementsByTagName("url"); for (int i = 0; i < nl.getLength(); i++) { Node nd = nl.item(i); if(nd instanceof Element) { Element el = (Element) nd; NodeList nodes = el.getChildNodes(); for (int j = 0; j < nodes.getLength(); j++) { Node node = nodes.item(j); if (node instanceof Text) { String dt = ((Text)node).getData(); byte[] b = Base64.decode(dt, Base64.DEFAULT); dt = new String(b); urlList.add(dt); urlNodes.add((Element) el.getParentNode()); Node kid = el.getFirstChild(); while (kid != null) { if (childName.equals(kid.getNodeName())) { Node nn = kid.getFirstChild(); if (nn instanceof Text) { String dt = ((Text)nn).getData(); return Base64.decode(dt, Base64.DEFAULT); try { Element ar = (Element) myDoc.getDocumentElement().getFirstChild().getFirstChild(); byte b[] = getElBytes(ar, "data");
).newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(reader)); doc.getDocumentElement().normalize(); System.out.println("Dom Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("node"); for (int temp = 0; temp < nList.getLength();temp++) { Element element = (Element) nList.item(temp); NodeList time = element.getElementsByTagName("createTime"); Element line = (Element) time.item(0); try { String value =getDataFromElement(line); Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData();
/** * Assumes tag only occurs once and contains only text / CDATA * If tag does not exist 'nullable' determines if an Exception is thrown */ public static String getText(Element element, String elementName, boolean nullable) throws Exception { NodeList nl = element.getElementsByTagName(elementName); if(nl.getLength()<1) { if(nullable) { return null; }else { throw(new Exception("Cannot find " + elementName + " tag")); } } Element stmt = (Element)nl.item(0); stmt.normalize(); nl = stmt.getChildNodes(); if(nl.getLength() !=1 ) { throw(new Exception("Cannot process " + elementName + " tag")); } Node n = nl.item(0); if(n.getNodeType()!=Node.TEXT_NODE && n.getNodeType()!=Node.CDATA_SECTION_NODE) { throw(new Exception("Cannot process " + elementName + " tag")); } return ((CharacterData)n).getData().trim(); }
/** * Assumes tag only occurs once and contains only text / CDATA * If tag does not exist 'nullable' determines if an Exception is thrown * */ public static String getText(String elementName,Document doc, boolean nullable) throws Exception { NodeList nl = doc.getElementsByTagName(elementName); if(nl.getLength()<1) { if(nullable) { return null; }else { throw(new Exception("Cannot find " + elementName + " tag")); } } Element stmt = (Element)nl.item(0); stmt.normalize(); nl = stmt.getChildNodes(); if(nl.getLength() !=1 ) { throw(new Exception("Cannot process " + elementName + " tag")); } Node n = nl.item(0); if(n.getNodeType()!=Node.TEXT_NODE && n.getNodeType()!=Node.CDATA_SECTION_NODE) { throw(new Exception("Cannot process " + elementName + " tag")); } return ((CharacterData)n).getData().trim(); }
/** * Runs the test case. * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; Element root; NodeList elementList; Node firstChild; NodeList textList; CharacterData textNode; String data; doc = (Document) load("staff", false); root = doc.getDocumentElement(); root.normalize(); elementList = root.getElementsByTagName("name"); firstChild = elementList.item(2); textList = firstChild.getChildNodes(); textNode = (CharacterData) textList.item(0); data = textNode.getData(); assertEquals("data", "Roger\n Jones", data); } /**
String piDataVal; doc = (Document) load("staffNS", true); domImpl = doc.getImplementation(); docType = doc.getDoctype(); docImp = domImpl.createDocument("http://www.w3.org/DOM/Test", "a:b", docTypeNull); nodeMap = docType.getEntities(); entity4 = (Entity) nodeMap.getNamedItem("ent4"); entityImp4 = (Entity) docImp.importNode(entity4, true); childList = entityImp4.getChildNodes(); element = (Element) childList.item(0); elemchildList = element.getChildNodes(); cdata = (CharacterData) elemchildList.item(0); pi = (ProcessingInstruction) childList.item(1); ent4Name = entity4.getNodeName(); ent4ImpName = entityImp4.getNodeName(); cdataVal = cdata.getData(); piTargetVal = pi.getTarget(); piDataVal = pi.getData();
static private void mergeAdjacentText(Node parent, StringBuilder collectorBuf) { Node child = parent.getFirstChild(); while (child != null) { Node next = child.getNextSibling(); if (child instanceof Text) { boolean atFirstText = true; while (next instanceof Text) { // if (atFirstText) { collectorBuf.setLength(0); collectorBuf.ensureCapacity(child.getNodeValue().length() + next.getNodeValue().length()); collectorBuf.append(child.getNodeValue()); atFirstText = false; } collectorBuf.append(next.getNodeValue()); parent.removeChild(next); next = child.getNextSibling(); } if (!atFirstText && collectorBuf.length() != 0) { ((CharacterData) child).setData(collectorBuf.toString()); } } else { mergeAdjacentText(child, collectorBuf); } child = next; } }
switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: AttrImpl attr = (AttrImpl) node; return createCDATASection(((CharacterData) node).getData()); case Node.DOCUMENT_TYPE_NODE: throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Cannot copy node of type " + node.getNodeType()); return createEntityReference(node.getNodeName());
/** * Handle TEXT nodes and CDATA nodes * * @param node * @param elementMap */ private void handleTextNode(Node node, Map<String, Object> elementMap) { Object text = ""; int nodeType = node.getNodeType(); switch (nodeType) { case Node.TEXT_NODE: text = normalizeText(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: text = normalizeText(((CharacterData) node).getData()); break; default: break; } // If the text is valid ... if (!StringUtils.isEmpty(text.toString())) { // We check if we have already collected some text previously Object previousText = elementMap.get("_text"); if (previousText != null) { // If we just have a "_text" key than we need to collect to a List text = Arrays.asList(previousText.toString(), text); } elementMap.put("_text", text); } }
/** * Concat all the text and cdata node children of this elem and return * the resulting text. * * @param parentEl the element whose cdata/text node values are to * be combined. * @return the concatanated string. */ static public String getChildCharacterData (Element parentEl) { if (parentEl == null) { return null; } Node tempNode = parentEl.getFirstChild(); StringBuffer strBuf = new StringBuffer(); CharacterData charData; while (tempNode != null) { switch (tempNode.getNodeType()) { case Node.TEXT_NODE : case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode; strBuf.append(charData.getData()); break; } tempNode = tempNode.getNextSibling(); } return strBuf.toString(); }