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=\"...\" />"); } } } } } } }
/** 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); }
/** * 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 Node findApplicationNode(Document manifestDocument) { NodeList applicationNodes = manifestDocument.getElementsByTagName("application"); if (applicationNodes.getLength() > 1) { throw new RuntimeException("found " + applicationNodes.getLength() + " application elements"); } return applicationNodes.item(0); }
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("document.xml")); NodeList nodeList = document.getElementsByTagName("*"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { // do something with the current element System.out.println(node.getNodeName()); } } }
protected void parseRules(InputStream input) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); // for now DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse(input); Element root=document.getDocumentElement(); match(RULES, root.getNodeName(), true); NodeList children=root.getChildNodes(); if(children == null || children.getLength() == 0) return; for(int i=0; i < children.getLength(); i++) { Node node=children.item(i); if(node.getNodeType() != Node.ELEMENT_NODE) continue; String element_name=node.getNodeName(); if(RULE.equals(element_name)) parseRule(node); else throw new Exception("expected <" + RULE + ">, but got " + "<" + element_name + ">"); } }
public KlattDurationParams(String filename) throws SAXException, IOException, ParserConfigurationException { // parse the xml file: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File(filename)); // In document, ignore everything that is not a segment element: NodeList segElements = document.getElementsByTagName("segment"); for (int i = 0; i < segElements.getLength(); i++) { Element seg = (Element) segElements.item(i); String name = seg.getAttribute("s"); int inherentDuration = Integer.parseInt(seg.getAttribute("inh")); int minimalDuration = Integer.parseInt(seg.getAttribute("min")); inh.put(name, inherentDuration); min.put(name, minimalDuration); } }
private void initData () throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbFactory.newDocumentBuilder(); Document doc = builder.parse(ExternalExtensionsDialog.class .getResourceAsStream("/com/badlogic/gdx/setup/data/extensions.xml")); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("extension"); for (int i = 0; i < nList.getLength(); i++) { Node nNode = nList.item(i); if (nNode.getNodeType() == Node.ELEMENT_NODE) { String name = eElement.getElementsByTagName("name").item(0).getTextContent(); String description = eElement.getElementsByTagName("description").item(0).getTextContent(); String version = eElement.getElementsByTagName("version").item(0).getTextContent(); String compatibility = eElement.getElementsByTagName("compatibility").item(0).getTextContent(); String url = eElement.getElementsByTagName("website").item(0).getTextContent(); NodeList inheritsNode = eElement.getElementsByTagName("inherit"); gwtInherits = new String[inheritsNode.getLength()]; for (int j = 0; j < inheritsNode.getLength(); j++) gwtInherits[j] = inheritsNode.item(j).getTextContent();
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(new File("document.xml")); doSomething(document.getDocumentElement()); } public static void doSomething(Node node) { // do something with the current node instead of System.out System.out.println(node.getNodeName()); NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node currentNode = nodeList.item(i); if (currentNode.getNodeType() == Node.ELEMENT_NODE) { //calls this method for all the children which is Element doSomething(currentNode); } } }
private AndroidManifest parse(File androidManifestFile, boolean libraryProject) throws AndroidManifestNotFoundException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); Element documentElement = doc.getDocumentElement(); documentElement.normalize(); String applicationPackage = documentElement.getAttribute("package"); NodeList sdkNodes = documentElement.getElementsByTagName("uses-sdk"); if (sdkNodes.getLength() > 0) { Node sdkNode = sdkNodes.item(0); minSdkVersion = extractAttributeIntValue(sdkNode, "android:minSdkVersion", -1); maxSdkVersion = extractAttributeIntValue(sdkNode, "android:maxSdkVersion", -1); boolean applicationDebuggableMode = false; if (applicationNodes.getLength() > 0) { Node applicationNode = applicationNodes.item(0); Node nameAttribute = applicationNode.getAttributes().getNamedItem("android:name"); LOGGER.warn("The class application declared in the AndroidManifest.xml cannot be found in the compile path: [{}]", nameAttribute.getNodeValue()); Node debuggableAttribute = applicationNode.getAttributes().getNamedItem("android:debuggable"); if (debuggableAttribute != null) { applicationDebuggableMode = debuggableAttribute.getNodeValue().equalsIgnoreCase("true");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); docBuilder.setEntityResolver((publicId, systemId) -> { Document doc = docBuilder.parse(svgFontFile); doc.getDocumentElement().normalize(); NodeList glyphsList = doc.getElementsByTagName("glyph"); for (int i = 0; i < glyphsList.getLength(); i++) { Node glyph = glyphsList.item(i); Node glyphName = glyph.getAttributes().getNamedItem("glyph-name"); if (glyphName == null) { continue; String glyphId = glyphName.getNodeValue(); SVGGlyphBuilder glyphPane = new SVGGlyphBuilder(i, glyphId, glyph.getAttributes() .getNamedItem("d") .getNodeValue());
private static String getDependencyManagementPluginVersion() { try (FileReader pomReader = new FileReader("pom.xml")) { Document pom = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(pomReader)); NodeList dependencyElements = pom.getElementsByTagName("dependency"); for (int i = 0; i < dependencyElements.getLength(); i++) { Element dependency = (Element) dependencyElements.item(i); if (dependency.getElementsByTagName("artifactId").item(0).getTextContent() .equals("dependency-management-plugin")) { return dependency.getElementsByTagName("version").item(0).getTextContent(); } } throw new IllegalStateException("dependency management plugin version not found"); } catch (Exception ex) { throw new IllegalStateException("Failed to find dependency management plugin version", ex); } }
HttpResponse response = client.execute(post); HttpEntity r_entity = response.getEntity(); String xmlString = EntityUtils.toString(r_entity); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xmlString)); Document doc = db.parse(inStream); String playcount = "empty"; NodeList nl = doc.getElementsByTagName("playcount"); for(int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i); playcount = nameElement.getFirstChild().getNodeValue().trim(); } }
private static Source mergeIntoSource(InputStream baseXmlStream, InputStream xmlStream) throws Exception { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setIgnoringComments(true); domFactory.setValidating(false); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document baseDoc = builder.parse(baseXmlStream); Document ruleDoc = builder.parse(xmlStream); // Shall this be more generic, i.e. reuse not just unification ??? NodeList unificationNodes = baseDoc.getElementsByTagName("unification"); Node ruleNode = ruleDoc.getElementsByTagName("rules").item(0); Node firstChildRuleNode = ruleNode.getChildNodes().item(1); for (int i = 0; i < unificationNodes.getLength(); i++) { Node unificationNode = ruleDoc.importNode(unificationNodes.item(i), true); ruleNode.insertBefore(unificationNode, firstChildRuleNode); } return new DOMSource(ruleDoc); }
private void parse() throws ParserException { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); Document document; try { DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { document = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); Node manifestNode = document.getElementsByTagName("manifest").item(0); NodeList nodes = manifestNode.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); String nodeName = node.getNodeName(); if (nodeName.equals("application")) { NodeList children = node.getChildNodes(); for (int j = 0; j < children.getLength(); j++) { Node child = children.item(j); String childName = child.getNodeName(); switch (childName) {
List<Triple<String, Element, String>> sents = Generics.newArrayList(); try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(f); doc.getDocumentElement().normalize(); NodeList nodeList=doc.getElementsByTagName(tag); for (int i = 0; i < nodeList.getLength(); i++) { Node prevNode = nodeList.item(i).getPreviousSibling(); String prev = ""; int count = 0; while (prevNode != null && count <= numIncludedSiblings) { prev = prevNode.getTextContent() + prev; prevNode = prevNode.getPreviousSibling(); count++; Node nextNode = nodeList.item(i).getNextSibling(); String next = ""; count = 0; count++; Element element = (Element)nodeList.item(i); Triple<String, Element, String> t = new Triple<>(prev, element, next); sents.add(t);
private void compileMappingFiles(List<String> mappingFiles, byte[] sourceArray) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); // Disable DTDs to prevent XXE attack dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder parser = dbf.newDocumentBuilder(); parser.setErrorHandler(handler); Document dom = parser.parse(new ByteArrayInputStream(sourceArray)); NodeList nodes = dom.getElementsByTagName("/persistence/persistence-unit/mapping-file"); if (nodes != null && nodes.getLength() > 0) { int length = nodes.getLength(); for (int j=0;j<length;j++){ Node node = nodes.item(j); mappingFiles.add(node.getNodeValue()); } } } }
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Demo { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new File("input.xml")); NodeList nodeList = document.getElementsByTagName("Item"); for(int x=0,size= nodeList.getLength(); x<size; x++) { System.out.println(nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue()); } } }
//obtain Document somehow, doesn't matter how DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); org.w3c.dom.Document doc = b.parse(new FileInputStream("page.html")); //Evaluate XPath against Document itself XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList)xPath.evaluate("/html/body/p/div[3]/a", doc.getDocumentElement(), XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); ++i) { Element e = (Element) nodes.item(i); }
private final String parseFingerprint(final String fingerprint) throws AuthorizationAccessException { final byte[] fingerprintBytes = fingerprint.getBytes(StandardCharsets.UTF_8); try (final ByteArrayInputStream in = new ByteArrayInputStream(fingerprintBytes)) { final DocumentBuilder docBuilder = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); final Document document = docBuilder.parse(in); final Element rootElement = document.getDocumentElement(); final NodeList userGroupProviderList = rootElement.getElementsByTagName(USER_GROUP_PROVIDER_ELEMENT); if (userGroupProviderList.getLength() != 1) { throw new AuthorizationAccessException(String.format("Only one %s element is allowed: %s", USER_GROUP_PROVIDER_ELEMENT, fingerprint)); } final Node userGroupProvider = userGroupProviderList.item(0); return userGroupProvider.getTextContent(); } catch (SAXException | ParserConfigurationException | IOException e) { throw new AuthorizationAccessException("Unable to parse fingerprint", e); } } }