/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
private String getTextValue(String def, Element doc, String tag) { String value = def; NodeList nl; nl = doc.getElementsByTagName(tag); if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) { value = nl.item(0).getFirstChild().getNodeValue(); } return value; }
/** * Retrieves all child elements of the given DOM element. * @param ele the DOM element to analyze * @return a List of child {@code org.w3c.dom.Element} instances */ public static List<Element> getChildElements(Element ele) { Assert.notNull(ele, "Element must not be null"); NodeList nl = ele.getChildNodes(); List<Element> childEles = new ArrayList<>(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { childEles.add((Element) node); } } return childEles; }
private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) { if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { if ("property".equals(node.getNodeName()) || "property".equals(node.getLocalName())) { String name = ((Element) node).getAttribute("name"); if (name != null && name.length() > 0) { String value = ((Element) node).getAttribute("value"); String ref = ((Element) node).getAttribute("ref"); if (value != null && value.length() > 0) { beanDefinition.getPropertyValues().addPropertyValue(name, value); } else if (ref != null && ref.length() > 0) { beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref)); } else { throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />"); } } } } } } }
private void addToDependencyMapFromXML (Map<String, List<ExternalExtensionDependency>> dependencies, Element eElement, String platform) { if (eElement.getElementsByTagName(platform).item(0) != null) { Element project = (Element)eElement.getElementsByTagName(platform).item(0); ArrayList<ExternalExtensionDependency> deps = new ArrayList<ExternalExtensionDependency>(); if (project.getTextContent().trim().equals("")) { // No dependencies required } else if (project.getTextContent().trim().equals("null")) { // Not supported deps = null; } else { NodeList nList = project.getElementsByTagName("dependency"); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element dependencyNode = (Element)nNode; boolean external = Boolean.parseBoolean(dependencyNode.getAttribute("external")); deps.add(new ExternalExtensionDependency(dependencyNode.getTextContent(), external)); } } } dependencies.put(platform, deps); } }
public static void removeChildren(Node e) { NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); e.removeChild(n); } } private static void getMatchingNodes(Node node, Pattern[] nodePath, int cur, List<Node> res) {
private List<String> extractUsesPermissionNames(NodeList usesPermissionNodes) { List<String> usesPermissionQualifiedNames = new ArrayList<>(); for (int i = 0; i < usesPermissionNodes.getLength(); i++) { Node usesPermissionNode = usesPermissionNodes.item(i); Node nameAttribute = usesPermissionNode.getAttributes().getNamedItem("android:name"); if (nameAttribute == null) { return null; } usesPermissionQualifiedNames.add(nameAttribute.getNodeValue()); } return usesPermissionQualifiedNames; }
private static String getChildElementValue( final String tagName, final Element element) { Node tagNode = element.getElementsByTagName(tagName).item(0); if ( tagNode == null ) return null; NodeList nodes= tagNode.getChildNodes(); Node node = (Node)nodes.item(0); return node.getNodeValue(); }
static String extractEncryptPart(String xml) { try { DocumentBuilder db = builderLocal.get(); Document document = db.parse(new InputSource(new StringReader(xml))); Element root = document.getDocumentElement(); return root.getElementsByTagName("Encrypt").item(0).getTextContent(); } catch (Exception e) { throw new RuntimeException(e); } }
/** If the child node doesn't exist, it is created. */ private static Node getFirstChildNodeByName (Node parent, String child) { NodeList childNodes = parent.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeName().equals(child)) { return childNodes.item(i); } } Node newNode = parent.getOwnerDocument().createElement(child); if (childNodes.item(0) != null) return parent.insertBefore(newNode, childNodes.item(0)); else return parent.appendChild(newNode); }
/** * Extracts the text value from the given DOM element, ignoring XML comments. * <p>Appends all CharacterData nodes and EntityReference nodes into a single * String value, excluding Comment nodes. Only exposes actual user-specified * text, no default values of any kind. * @see CharacterData * @see EntityReference * @see Comment */ public static String getTextValue(Element valueEle) { Assert.notNull(valueEle, "Element must not be null"); StringBuilder sb = new StringBuilder(); NodeList nl = valueEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node item = nl.item(i); if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) { sb.append(item.getNodeValue()); } } return sb.toString(); }
protected String getString(String tagName, Element element) { NodeList list = element.getElementsByTagName(tagName); if (list != null && list.getLength() > 0) { NodeList subList = list.item(0).getChildNodes(); if (subList != null && subList.getLength() > 0) { return subList.item(0).getNodeValue(); } } return null; }
private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) { if (nodeList != null && nodeList.getLength() > 0) { for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { if ("property".equals(node.getNodeName()) || "property".equals(node.getLocalName())) { String name = ((Element) node).getAttribute("name"); if (name != null && name.length() > 0) { String value = ((Element) node).getAttribute("value"); String ref = ((Element) node).getAttribute("ref"); if (value != null && value.length() > 0) { beanDefinition.getPropertyValues().addPropertyValue(name, value); } else if (ref != null && ref.length() > 0) { beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref)); } else { throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />"); } } } } } } }
/** * Reconstructs the resource from the XML file */ @SuppressWarnings("unchecked") public SsurgeonWordlist(Element rootElt) { id = rootElt.getAttribute("id"); NodeList wordEltNL = rootElt.getElementsByTagName(WORD_ELT); for (int i=0; i<wordEltNL.getLength(); i++) { Node node = wordEltNL.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { String word = Ssurgeon.getEltText((Element) node); words.add(word); } } }
private void populateContent(Node item, Object object) { for (int i = 0; i < item.getChildNodes().getLength(); i++) { Node child = item.getChildNodes().item(i); if (child instanceof Text) { setText(object, (Text) child); } } }
protected void parseCollectionElements( NodeList elementNodes, Collection<Object> target, @Nullable BeanDefinition bd, String defaultElementType) { for (int i = 0; i < elementNodes.getLength(); i++) { Node node = elementNodes.item(i); if (node instanceof Element && !nodeNameEquals(node, DESCRIPTION_ELEMENT)) { target.add(parsePropertySubElement((Element) node, bd, defaultElementType)); } } }
public void generate(Writer output) throws IOException { Document doc = (Document) template.cloneNode(true); transformHead((Element) doc.getDocumentElement().getElementsByTagName("head").item(0)); transformBody((Element) doc.getDocumentElement().getElementsByTagName("body").item(0)); encodeDocument(doc, output); }
private void parse(Document doc) { NodeList nodeList = doc.getChildNodes(); for (int count = 0; count < nodeList.getLength(); count++) { Node node = nodeList.item(count); if (node.getNodeType() == Node.ELEMENT_NODE && node.hasChildNodes()) { parseAttrList(node.getChildNodes()); } } }
/** * Utility method that returns the first child element identified by its name. * @param ele the DOM element to analyze * @param childEleName the child element name to look for * @return the {@code org.w3c.dom.Element} instance, or {@code null} if none found */ @Nullable public static Element getChildElementByTagName(Element ele, String childEleName) { Assert.notNull(ele, "Element must not be null"); Assert.notNull(childEleName, "Element name must not be null"); NodeList nl = ele.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element && nodeNameMatch(node, childEleName)) { return (Element) node; } } return null; }
public String getSample( String strFunctionName, String strFunctionNameWithArgs ) { String sRC = "// Sorry, no Script available for " + strFunctionNameWithArgs; NodeList nl = dom.getElementsByTagName( "jsFunction" ); for ( int i = 0; i < nl.getLength(); i++ ) { if ( nl.item( i ).getAttributes().getNamedItem( "name" ).getNodeValue().equals( strFunctionName ) ) { Node elSample = ( (Element) nl.item( i ) ).getElementsByTagName( "sample" ).item( 0 ); if ( elSample.hasChildNodes() ) { return ( elSample.getFirstChild().getNodeValue() ); } } } return sRC; }