// XMLToStringConverter private static String returnResultFromXML(String convertDocToString) throws SAXException, IOException, ParserConfigurationException { String alchemyResult = null; // XML Conversion DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // Load the input XML document, parse it and return an instance of the // Document class. InputSource is = new InputSource(); is.setCharacterStream(new StringReader(convertDocToString)); Document document = builder.parse(is); NodeList nodes = document.getElementsByTagName("results"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NodeList authorElement = element.getElementsByTagName("author"); Element line = (Element) authorElement.item(i); // System.out.println("author is" + authorElement.toString()); Node child = line.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; alchemyResult = cd.getData(); } } return alchemyResult; }
/** * Gets the character data from element. * * @param e the e * @return the character data from element */ public static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return ""; }
private String getCharacterDataFromElement(Element e) { try { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } } catch (Exception ignored) { } return ""; }
public static List<String> getStringFromElement(Element e) { NodeList childList = e.getChildNodes(); ArrayList<String> elementValues = new ArrayList<String>(); for (int i = 0; i < childList.getLength(); i++) { Node noodi = childList.item(i); if (noodi instanceof CharacterData) { CharacterData cd = (CharacterData) noodi; elementValues.add(cd.getData()); } } return elementValues; }
/** * Returns value/cdata from xml node. * * @param node * @return String */ public static String getNodeValue(Node node) { String value = node.getNodeValue(); if (value == null) { Node nChild = node.getFirstChild(); if (nChild instanceof CharacterData) { CharacterData cd = (CharacterData) nChild; value = cd.getData(); } } return value; }
public static String getCharacterDataFromElement(Element e) { for (int i = 0; i < e.getChildNodes().getLength(); i++) { Node child = e.getChildNodes().item(i); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; String result = cd.getData().replaceAll(" ", ""); result = result.replaceAll("\n", ""); if (!result.equals("")) { return cd.getData(); } } } return ""; }
public void setDefaultDataModel(Object dataModel) { if (markupDrivenUtil != null) { HTMLTextAreaElement elem = getHTMLTextAreaElement(); CharacterData text = (CharacterData) elem.getFirstChild(); String data; if (text != null) data = text.getData(); else data = ""; DOMUtilInternal.setAttribute(elem, "value", data); } if (markupDrivenUtil != null) markupDrivenUtil.preSetDefaultDataModel(dataModel); super.setDefaultDataModel(dataModel); }
/** * 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("staff", true); elementList = doc.getElementsByTagName("address"); nameNode = elementList.item(0); child = (CharacterData) nameNode.getFirstChild(); child.deleteData(0, 16); childData = child.getData(); assertEquals("characterdataDeleteDataBeginingAssert", "Dallas, Texas 98551", childData); }
/** * Runs the test case. * * @throws Throwable Any uncaught exception causes test to fail */ public void runTest() throws Throwable { Document doc; NodeList genderList; Node genderNode; Node entText; EntityReference entReference; Node appendedChild; doc = (Document) load("staff", true); genderList = doc.getElementsByTagName("gender"); genderNode = genderList.item(2); entReference = doc.createEntityReference("ent3"); assertNotNull("createdEntRefNotNull", entReference); appendedChild = genderNode.appendChild(entReference); entText = entReference.getFirstChild(); assertNotNull("entTextNotNull", entText); { boolean success = false; try { ((CharacterData) /*Node */ entText).deleteData(1, 3); } catch (DOMException ex) { success = (ex.code == DOMException.NO_MODIFICATION_ALLOWED_ERR); } assertTrue("throw_NO_MODIFICATION_ALLOWED_ERR", success); } }
public void handleEvent(Event evt) { super.handleEvent(evt); ItsNatHTMLSelectMultImpl comp = getItsNatHTMLSelectMult(); // if (comp.isServerUpdatingFromClient()) // return; String type = evt.getType(); if (type.equals("DOMCharacterDataModified")) { MutationEvent mutEvent = (MutationEvent) evt; CharacterData charDataNode = (CharacterData) mutEvent.getTarget(); HTMLOptionElement option = (HTMLOptionElement) charDataNode.getParentNode(); int index = option.getIndex(); String value = charDataNode.getData(); DefaultListModel dataModel = (DefaultListModel) comp.getListModel(); if (!value.equals(dataModel.get(index))) dataModel.set(index, value); } }
/** * 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; doc = (Document) load("staff", true); elementList = doc.getElementsByTagName("address"); nameNode = elementList.item(0); child = (CharacterData) nameNode.getFirstChild(); { boolean success = false; try { child.replaceData(40, 3, "ABC"); } catch (DOMException ex) { success = (ex.code == DOMException.INDEX_SIZE_ERR); } assertTrue("throw_INDEX_SIZE_ERR", success); } }
/** * Removes comments and processing instruction, and then unites adjacent text nodes. Note that * CDATA sections count as text nodes. */ public static void simplify(Node node) { NodeList children = node.getChildNodes(); int i = 0; int len = children.getLength(); Node prevTextChild = null; while (i < len) { Node child = children.item(i); if (child.hasChildNodes()) { simplify(child); prevTextChild = null; i++; } else { int type = child.getNodeType(); if (type == Node.PROCESSING_INSTRUCTION_NODE) { node.removeChild(child); len--; } else if (type == Node.COMMENT_NODE) { node.removeChild(child); len--; } else if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) { if (prevTextChild != null) { CharacterData ptc = (CharacterData) prevTextChild; ptc.setData(ptc.getNodeValue() + child.getNodeValue()); node.removeChild(child); len--; } else { prevTextChild = child; i++; } } else { prevTextChild = null; i++; } } } }
/** * Merges adjacent text/cdata nodes, so that there are no adjacent text/cdata nodes. Operates * recursively on the entire subtree. You thus lose information about any CDATA sections occurring * in the doc. * * @see #simplify */ public static void mergeAdjacentText(Node node) { Node child = node.getFirstChild(); while (child != null) { if (child instanceof Text || child instanceof CDATASection) { Node next = child.getNextSibling(); if (next instanceof Text || next instanceof CDATASection) { String fullText = child.getNodeValue() + next.getNodeValue(); ((CharacterData) child).setData(fullText); node.removeChild(next); } } else { mergeAdjacentText(child); } child = child.getNextSibling(); } }