DocumentBuilder parser = factory.newDocumentBuilder(); Document doc = parser.parse(modelStream); Element rootNode = doc.getDocumentElement(); if (rootNode.getNodeName().equals("entries")) { NodeList entriesList = rootNode.getChildNodes(); for (int i = 0; i < entriesList.getLength(); i++) { if (entriesList.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { Element entry = (Element) entriesList.item(i); NodeList keyList = entry.getElementsByTagName("key"); NodeList valueList = entry.getElementsByTagName("value"); if (keyList.getLength() == 1 && valueList.getLength() == 1) { for (int a = 0; a < keyTextNodeList.getLength(); a++) { if (keyTextNodeList.item(a).getNodeType() == org.w3c.dom.Node.TEXT_NODE) { org.w3c.dom.Text textNode = (org.w3c.dom.Text) keyTextNodeList.item(a); keyString += textNode.getData().trim(); for (int a = 0; a < valueTextNodeList.getLength(); a++) { if (valueTextNodeList.item(a).getNodeType() == org.w3c.dom.Node.TEXT_NODE) { org.w3c.dom.Text textNode = (org.w3c.dom.Text) valueTextNodeList.item(a); valueString += textNode.getData().trim();
NodeList nl = orig.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i) instanceof DocumentType) { doctype = (DocumentType) nl.item(i); if (doctype != null) { doc = builder.getDOMImplementation().createDocument( orig.getDocumentElement().getNamespaceURI(), orig.getDocumentElement().getTagName(), builder.getDOMImplementation().createDocumentType( orig.getDoctype().getName(), Element e = (Element) nl.item(i); removeXmlBase(e); NodeList nl2 = e.getChildNodes(); for (int j = 0; j < nl2.getLength(); j++) { Node n = nl2.item(j); if (n instanceof Text && ((Text) n).getNodeValue().trim().length() == 0) { e.removeChild(n);
@Override public void characters(char[] ch, int start, int length) { String data = new String(ch, start, length); Node parent = getParent(); Node lastChild = parent.getLastChild(); if (lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) { ((Text) lastChild).appendData(data); } else { Text text = this.document.createTextNode(data); parent.appendChild(text); } }
/** * Go through all text nodes below this node, and replace their text with a trimmed version of their text. This changes the * DOM document. * * @param root * root */ public static void trimAllTextNodes(Node root) { Document doc = root.getNodeType() == Node.DOCUMENT_NODE ? (Document) root : root.getOwnerDocument(); NodeIterator it = ((DocumentTraversal) doc).createNodeIterator(root, NodeFilter.SHOW_TEXT, null, false); Text t = null; while ((t = (Text) it.nextNode()) != null) { String s = t.getData(); t.setData(s.trim()); } }
if (!t.getNodeName().equals(MaryXML.TOKEN)) throw new DOMException(DOMException.INVALID_ACCESS_ERR, "Only " + MaryXML.TOKEN + " elements allowed, received " + t.getNodeName() + "."); Document doc = t.getOwnerDocument(); NodeIterator textIt = ((DocumentTraversal) doc).createNodeIterator(t, NodeFilter.SHOW_TEXT, null, false); Text text = null; String textString = null; while ((text = (Text) textIt.nextNode()) != null) { textString = text.getData().trim(); if (!textString.equals("")) break; text = (Text) t.getOwnerDocument().createTextNode(s); t.appendChild(text); } else { // found the one text element with non-whitespace content text.setData(s);
protected void readDataInputAssociation(org.w3c.dom.Node xmlNode, SubProcessNode subProcessNode, Map<String, String> dataInputs) { org.w3c.dom.Node subNode = xmlNode.getFirstChild(); if ("sourceRef".equals(subNode.getNodeName())) { String from = subNode.getTextContent(); subNode = subNode.getNextSibling(); subNode = subNode.getNextSibling(); if (subNode != null && "transformation".equals(subNode.getNodeName())) { String lang = subNode.getAttributes().getNamedItem("language").getNodeValue(); String expression = subNode.getTextContent(); org.w3c.dom.Node subSubNode = subNode.getFirstChild(); NodeList nl = subSubNode.getChildNodes(); if (nl.getLength() > 1) { subProcessNode.addInMapping(dataInputs.get(to), subSubNode.getTextContent()); return; } else if (nl.getLength() == 0) { return; Object from = nl.item(0); if (from instanceof Text) { result = ((Text) from).getTextContent(); } else { result = nl.item(0);
trimEmptyTextNodes(actual); if (expected.getNodeType() != actual.getNodeType()) { throw new Exception("Different types of nodes: " + expected + " " + actual); Document expectedDoc = (Document) expected; Document actualDoc = (Document) actual; compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement(), false); } else if (expected instanceof Element) { Element expectedElement = (Element) expected; if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) { throw new Exception("Element names do not match: " + expectedElement.getLocalName() + " " + actualElement.getLocalName()); + countNonNamespaceAttributes(expectedAttrs) + " " + countNonNamespaceAttributes(actualAttrs)); for (int i = 0; i < expectedAttrs.getLength(); i++) { Attr expectedAttr = (Attr) expectedAttrs.item(i); if (expectedAttr.getName().startsWith("xmlns")) { continue; actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName()); if (actualAttr == null) { throw new Exception(elementName + ": No attribute found:" + expectedAttr); String expectedData = ((Text) expected).getData().trim(); String actualData = ((Text) actual).getData().trim();
private void serializeToBuffer(final Element el, final StringBuffer buf) { buf.append("<").append(el.getTagName()); final NamedNodeMap attributeMap = el.getAttributes(); for (int i = 0; i < attributeMap.getLength(); i++) { final org.w3c.dom.Node node = attributeMap.item(i); if (node.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE) { final Attr attr = (Attr) node; buf.append(" ").append(attr.getName()).append("=\"").append(attr.getValue()).append("\""); } } buf.append(">"); final NodeList nodeList = el.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { final org.w3c.dom.Node node = nodeList.item(i); if (node.getNodeType() == org.w3c.dom.Node.TEXT_NODE) { final Text text = (Text) node; buf.append(text.getData()); } else if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { final Element childEl = (Element) node; serializeToBuffer(childEl, buf); } } buf.append("</").append(el.getTagName()).append(">"); }
private static void copyDocument (Document doc, Element from, Element to) { NodeList nl = from.getChildNodes(); int length = nl.getLength(); for (int i=0; i< length; i++) { Node node = nl.item (i); Node newNode = null; switch (node.getNodeType()) { case Node.ELEMENT_NODE: Element oldElement = (Element) node; newNode = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,oldElement.getTagName()); NamedNodeMap m = oldElement.getAttributes(); Element newElement = (Element) newNode; for (int index = 0; index < m.getLength(); index++) { Node attr = m.item(index); newElement.setAttribute(attr.getNodeName(), attr.getNodeValue()); case Node.TEXT_NODE: Text oldText = (Text) node; newNode = doc.createTextNode(oldText.getData()); break; case Node.COMMENT_NODE: Comment oldComment = (Comment) node; newNode = doc.createComment(oldComment.getData()); break;
public static String getTextContent(Element element) { final NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { final Node node = childNodes.item(i); if (node instanceof Text) { String textContent = ((Text) node).getWholeText(); textContent = textContent.trim(); return textContent; } } return null; }
SortedNode(Element e) { this.name = e.getNodeName(); NamedNodeMap attrs = e.getAttributes(); if (attrs != null) { StringBuilder sb = new StringBuilder(); Set<String> attrNames = new TreeSet<>(); for (int i = 0; i < attrs.getLength(); i++) attrNames.add(attrs.item(i).getNodeName()); for (String n : attrNames) { Node node = attrs.getNamedItem(n); sb.append(" ").append(n).append("='").append(node.getNodeValue()).append("'"); } this.attrs = sb.toString(); } NodeList nl = e.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n instanceof Element) children.add(new SortedNode((Element)nl.item(i))); if (n instanceof Text) this.text += ((Text)n).getNodeValue(); } Collections.sort(children); }
NodeList docNodes = docElem.getChildNodes(); Element textElem = null; for (int i = 0; i < docNodes.getLength(); i++) { Node n = docNodes.item(i); if ("text".equals(n.getNodeName())) { textElem = (Element) n; break; NodeList textNodes = textElem.getChildNodes(); for (int i = 0; i < textNodes.getLength(); i++) { Node content = textNodes.item(i); if (content instanceof Text) { Text text = (Text)content; offset += text.getWholeText().length(); } else if (content instanceof Element) { Element child = (Element)content; if (child.getNodeName().equals("TIMEX3")) { Timex timex = new Timex(child); if (child.getChildNodes().getLength() != 1) {
NodeList nodelist = node.getChildNodes(); for (int i = 0; i < nodelist.getLength(); i++) Node child = nodelist.item(i); if (child instanceof Element) String cdata = ((Text) child).getData(); if (cdata!=null) NamedNodeMap attributes = node.getAttributes(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < attributes.getLength(); i++) Node attribute = attributes.item(i); String attributeNodeValue = attribute.getNodeValue(); if (attributeNodeValue!=null)
NodeList children = element.getChildNodes(); boolean hasChildren = (children.getLength() > 0); boolean hasChildElements = false; openElement(element, out, indent, indentWith, hasChildren); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); switch (child.getNodeType()) { out.write(encode(child.getNodeValue())); break; out.write(encode(child.getNodeValue())); out.write("-->"); break; encodedata(out, ((Text) child).getData()); out.write("]]>"); break;
Element element = null; if (node instanceof Document) { element = ((Document) node).getDocumentElement(); } else if (node instanceof Element) { element = (Element) node; NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Element) { trimEmptyTextNodes(child); } else if (child instanceof Text) { Text t = (Text) child; if (t.getData().trim().length() == 0) { nodesToRemove.add(child); element.removeChild(n);
parent = ((org.w3c.dom.Node) target).getParentNode(); Document doc = builder.newDocument(); Element temp = doc.createElementNS(null, "temp"); temp.appendChild(doc.createTextNode((String) source)); nl = temp.getChildNodes(); } else if (source == null) { if (nl.getLength() == 0) { throw new RuntimeException("Nothing was selected by the from expression " + from + " on " + sourceExpr); for (int i = 0 ; i < nl.getLength(); i++) { if (nl.item(i) instanceof Attr) { targetElem = ((Attr) nl.item(i)).getValue(); } else if (nl.item(i) instanceof Text) { targetElem = ((Text) nl.item(i)).getWholeText(); } else { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.newDocument(); targetElem = doc.importNode(nl.item(i), true); org.w3c.dom.Node n = ((org.w3c.dom.Node) targetElem).getOwnerDocument().importNode(nl.item(i), true); if (n instanceof Attr) { ((Element) targetElem).setAttributeNode((Attr) n); } else { ((org.w3c.dom.Node) targetElem).appendChild(n);
protected void readEndDataInputAssociation(org.w3c.dom.Node xmlNode, EndNode endNode) { org.w3c.dom.Node subNode = xmlNode.getFirstChild(); if ("sourceRef".equals(subNode.getNodeName())) { String eventVariable = subNode.getTextContent(); if (eventVariable != null && eventVariable.trim().length() > 0) { if (dataInputs.containsKey(eventVariable)) { org.w3c.dom.Node subSubNode = subNode.getFirstChild(); NodeList nl = subSubNode.getChildNodes(); if (nl.getLength() > 1) { endNode.setMetaData("MappingVariable", subSubNode.getTextContent()); return; } else if (nl.getLength() == 0) { return; Object from = nl.item(0); if (from instanceof Text) { String text = ((Text) from).getTextContent(); if (text.startsWith("\"") && text.endsWith("\"")) { result = text.substring(1, text.length() -1);
root = doc.getDocumentElement(); toAddTo = new Properties(); boolean finalParameter = false; LinkedList<String> source = new LinkedList<String>(); for (int j = 0; j < fields.getLength(); j++) { Node fieldNode = fields.item(j); if (!(fieldNode instanceof Element)) if ("name".equals(field.getTagName()) && field.hasChildNodes()) attr = StringInterner.weakIntern( ((Text)field.getFirstChild()).getData().trim()); if ("value".equals(field.getTagName()) && field.hasChildNodes()) value = StringInterner.weakIntern( ((Text)field.getFirstChild()).getData()); if ("final".equals(field.getTagName()) && field.hasChildNodes()) finalParameter = "true".equals(((Text)field.getFirstChild()).getData()); if ("source".equals(field.getTagName()) && field.hasChildNodes()) source.add(StringInterner.weakIntern( ((Text)field.getFirstChild()).getData()));
/** * Extract nested text from a node. * Currently does not handle coalescing text nodes, CDATA sections, etc. * @param parent a parent element * @return the nested text, or null if none was found * * @since 8.4 */ public static String findText(Node parent) { NodeList l = parent.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { if (l.item(i).getNodeType() == Node.TEXT_NODE) { Text text = (Text) l.item(i); return text.getNodeValue(); } } return null; }
/** * Find all direct child elements of an element. * Children which are all-whitespace text nodes or comments are ignored; others cause * an exception to be thrown. * @param parent a parent element in a DOM tree * @return a list of direct child elements (may be empty) * @throws IllegalArgumentException if there are non-element children besides whitespace * * @since 8.4 */ public static List<Element> findSubElements(Element parent) throws IllegalArgumentException { NodeList l = parent.getChildNodes(); List<Element> elements = new ArrayList<Element>(l.getLength()); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) n); } else if (n.getNodeType() == Node.TEXT_NODE) { String text = ((Text) n).getNodeValue(); if (text.trim().length() > 0) { throw new IllegalArgumentException("non-ws text encountered in " + parent + ": " + text); // NOI18N } } else if (n.getNodeType() == Node.COMMENT_NODE) { // OK, ignore } else { throw new IllegalArgumentException("unexpected non-element child of " + parent + ": " + n); // NOI18N } } return elements; }