List of usage examples for org.w3c.dom Element getFirstChild
public Node getFirstChild();
From source file:Main.java
/** * Return the first child Element with the given name; if name is null returns the first element. *//*from w w w .ja v a 2s. co m*/ public static Element firstChildElement(Element element, String childElementName, String attrName, String attrValue) { if (element == null) return null; // get the first element with the given name Node node = element.getFirstChild(); if (node != null) { do { if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null || childElementName.equals(node.getNodeName()))) { Element childElement = (Element) node; String value = childElement.getAttribute(attrName); if (value != null && value.equals(attrValue)) { return childElement; } } } while ((node = node.getNextSibling()) != null); } return null; }
From source file:com.microsoft.tfs.core.clients.build.internal.utils.XamlHelper.java
public static String updateProperties(final String originalXaml, final Properties properties) { final ArrayList keys = new ArrayList(properties.keySet()); final Document document = DOMCreateUtils.parseString(originalXaml); final Element root = document.getDocumentElement(); // first update any properties that we already have final NodeList nodes = root.getElementsByTagName("x:String"); //$NON-NLS-1$ for (int i = 0; i < nodes.getLength(); i++) { final Element element = (Element) nodes.item(i); final String key = element.getAttribute("x:Key"); //$NON-NLS-1$ element.getFirstChild().getNodeValue(); if (properties.containsKey(key)) { keys.remove(key);//from ww w . j a v a 2 s .com element.getFirstChild().setNodeValue(properties.getProperty(key)); } } // now add any new properties to the xaml for (final Iterator it = keys.iterator(); it.hasNext();) { final String key = (String) it.next(); final Element element = DOMUtils.appendChild(root, "x:String"); //$NON-NLS-1$ element.setAttributeNS(XAML_NAMESPACE, "x:Key", key); //$NON-NLS-1$ element.setAttributeNS(XML_NAMESPACE, "xml:space", "preserve"); //$NON-NLS-1$ //$NON-NLS-2$ DOMUtils.appendText(element, properties.getProperty(key)); } return DOMSerializeUtils.toString(root, DOMSerializeUtils.INDENT).trim(); }
From source file:DOMUtils.java
/** * Return the first child element of the given element. Null if no * children are found./*from w w w .j a v a 2 s . c o m*/ * * @param elem Element whose child is to be returned * @return the first child element. */ public static Element getFirstChildElement(Element elem) { for (Node n = elem.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE) { return (Element) n; } } return null; }
From source file:Main.java
public static List<Element> elementsQName(Element element, Set<QName> allowedTagNames) { if (element == null || !element.hasChildNodes()) { return Collections.emptyList(); }//from w ww . j av a 2s .c o m List<Element> elements = new ArrayList<Element>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) child; QName childQName = new QName(childElement.getNamespaceURI(), childElement.getLocalName()); if (allowedTagNames.contains(childQName)) { elements.add(childElement); } } } return elements; }
From source file:com.microsoft.tfs.core.clients.build.internal.utils.XamlHelper.java
public static Properties loadPartial(final String xaml) { final Properties properties = new Properties(); try {/*from w ww . ja v a 2 s . co m*/ final Document document = DOMCreateUtils.parseString(xaml); final Element root = document.getDocumentElement(); final NodeList nodes = root.getElementsByTagName("x:String"); //$NON-NLS-1$ for (int i = 0; i < nodes.getLength(); i++) { final Element element = (Element) nodes.item(i); final String key = element.getAttribute("x:Key"); //$NON-NLS-1$ final String value = element.getFirstChild().getNodeValue(); properties.put(key, value); } } catch (final XMLException e) { log.error( MessageFormat.format(Messages.getString("XamlHelper.ExceptionParsingProcessParaemtersFormat"), //$NON-NLS-1$ xaml), e); } return properties; }
From source file:Main.java
public static List<Element> elements(Element element) { if (element == null || !element.hasChildNodes()) { return Collections.emptyList(); }//from w ww . j a v a2s. c o m List<Element> elements = new ArrayList<Element>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) child); } } return elements; }
From source file:Main.java
private static boolean serializeXmlNode(Node node, Writer writer, boolean includeNode) throws IOException { if (node == null) { return false; }/* w ww . j av a 2 s . com*/ short type = node.getNodeType(); boolean result = true; switch (type) { case Node.ATTRIBUTE_NODE: { String text = ((Attr) node).getValue(); writer.write(text); break; } case Node.TEXT_NODE: { String text = ((Text) node).getData(); writer.write(text); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; if (includeNode) { serializeXML(element, writer, false); } else { Node child = element.getFirstChild(); while (child != null) { serializeXmlNode(child, writer, true); child = child.getNextSibling(); } } break; } case Node.DOCUMENT_NODE: { Document doc = (Document) node; serializeXmlNode(doc.getDocumentElement(), writer, includeNode); break; } default: result = false; break; } return result; }
From source file:Main.java
/** * Returns the text content of the provided element as is. If multiple text * DOM nodes are present, they are concatenated together. * /*ww w . ja v a 2 s .c o m*/ * @param element The element that contains the desired text content. * @return The text content of the given element. * @throws Exception If an exception occurs during the traversal of the DOM * objects. */ public static String getElementTextData(Element element) throws Exception { if (!element.hasChildNodes()) { throw new Exception("Element has no children."); } Node n = element.getFirstChild(); if ((n.getNodeType() != Node.TEXT_NODE) && (n.getNodeType() != Node.CDATA_SECTION_NODE)) { // must be a textual node // for now throw an exception, but later need to just skip this module and // resume initialization throw new Exception("Element child node is not textual."); } CharacterData value = (CharacterData) n; return value.getData(); }
From source file:com.microsoft.exchange.autodiscover.PoxAutodiscoverServiceImpl.java
/** * Static utility method to extract text from an XML element * @param e - an xml element/* w w w .j ava2 s . c o m*/ * @return the character datafrom the first child element as a string */ private static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return null; }
From source file:DOMUtils.java
/** * Return the first child element of the given element which has the * given attribute with the given value. * * @param elem the element whose children are to be searched * @param attrName the attrib that must be present * @param attrValue the desired value of the attribute * * @return the first matching child element. *//*from w ww. ja va 2 s .c o m*/ public static Element findChildElementWithAttribute(Element elem, String attrName, String attrValue) { for (Node n = elem.getFirstChild(); n != null; n = n.getNextSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE) { if (attrValue.equals(DOMUtils.getAttribute((Element) n, attrName))) { return (Element) n; } } } return null; }